[ILUG] sed question

Niall Sheridan nsheridan at gmail.com
Thu Aug 14 15:57:39 IST 2008


On 8/14/08, Marcus Furlong <furlongm at hotmail.com> wrote:
> Hi,
>
>  I'm having trouble using sed to do replacements on some badly tagged
>  xml. I have a large number of files that are tagged as follows:

Personally I'd use something like the python BeautifulSoup module to
do all the work. It's really forgiving when it comes to dealing with
poorly formed xml or html and makes it easy to pull information out of
pretty much any html/xml document.
Docs are here: http://www.crummy.com/software/BeautifulSoup/documentation.html

>>> import BeautifulSoup
>>>
>>> xml = """
... <first id="34">
... blah blah
... <second id="56" name="xyz1">hello hello</second>
... <second name="xyz4">hello hello</second>
... <second id="16" name="xyz5">hello hello</second>
... <first id="3">
... blah blah blah
... <second>hello hello</second>
... <second id="12" name="xyz5">hello hello</second>
... """
>>> soup = BeautifulSoup.BeautifulStoneSoup(xml)
>>> print soup.prettify()
<first id="34">
 blah blah
 <second id="56" name="xyz1">
  hello hello
 </second>
 <second name="xyz4">
  hello hello
 </second>
 <second id="16" name="xyz5">
  hello hello
 </second>
</first>
<first id="3">
 blah blah blah
 <second>
  hello hello
 </second>
 <second id="12" name="xyz5">
  hello hello
 </second>
</first>
>>> soup.findAll('second')
[<second id="56" name="xyz1">hello hello</second>, <second
name="xyz4">hello hello</second>, <second id="16" name="xyz5">hello
hello</second>, <second>hello hello</second>, <second id="12"
name="xyz5">hello hello</second>]
>>> for item in soup.findAll('second'):
...     print item
...
<second id="56" name="xyz1">hello hello</second>
<second name="xyz4">hello hello</second>
<second id="16" name="xyz5">hello hello</second>
<second>hello hello</second>
<second id="12" name="xyz5">hello hello</second>

 - Niall



More information about the ILUG mailing list