dealing with nested xml within nested xml within......
Stefan Behnel
stefan.behnel-n05pAM at web.de
Mon Jul 9 17:00:32 EDT 2007
Ultrus wrote:
> I don't need specific examples, but I'm trying to wrap my head around
> parsing xml within xml and even further, not limiting how far someone
> will nest xml. I'm already making great use of BeautifulSoup's
> BeautifulStoneSoup to parse xml, but what do I do if I come across
> something like this?
>
> <random>
> <li>This is a random response (once parsed)</li>
> <li>
> <random>
> <li>This is a random response within a random response</li>
> <li>
> <random>
> <li>This is a random response within a random response,
> within another random response</li>
> <li>Like above, this is another random response.</li>
> </random>
> </li>
> </random>
> </li>
> </random>
>
> Not knowing how far one will nest random responses, how would one
> manage digging into xml like this?
I don't know what you want to do with this document, but you might want to
consider using lxml.etree to handle it:
>>> from lxml import etree
>>> tree = etree.parse("myfile.xml")
>>> for random in tree.getiterator("random"):
... for li in random:
... if li.text.strip():
... print li.text
http://codespeak.net/lxml/
Stefan
More information about the Python-list
mailing list