dealing with nested xml within nested xml within......
kyosohma at gmail.com
kyosohma at gmail.com
Mon Jul 9 16:18:42 EDT 2007
On Jul 9, 3:03 pm, Ultrus <ownthe... at gmail.com> wrote:
> Hello all,
> 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? Right now I'm thinking about not
> even going there. I would presently write scripts that would parse 3
> or so levels deep, but no further. :P It would make an interesting
> project, like an interactive adventure story.
You'd probably write a function that called itself to parse something
like this. Unfortunately, I am not a recursion expert. You can read up
on it though:
http://www.freenetpages.co.uk/hp/alan.gauld/tutrecur.htm
http://pythonjournal.cognizor.com/pyj2.2/RecursionByAJChung.html
I haven't used Beautiful Soup, but I think you can use lxml or
ElementTree to get a tree object of the XML and then just iterate over
the tree.
Mike
More information about the Python-list
mailing list