[xml] listing descendant nodes in Python


I want to return a list of all descendant nodes for an element.
Using Python 2.2, it is possible for me very simply to do this:

import libxml2
doc = libxml2.parseFile("c:\\test.xml")
res = doc.xincludeProcess()
current_nodes = doc.xpathEval("descendant-or-self::some_element")
listing = list(current_nodes[0].children)
for a in listing:
    print a

I discovered that this is not a very portable (and probably wrong)
solution to the problem. Python 2.1 will not parse 'listing' as a list,
for example. It is kind of sad, because I found this trick very useful for
'grabbing' the descendants of any element and listing them. Is there a
better way that is still simple and doesn't require a large rewrite of


This email was sent using DACafeMail.
Get Your FREE 25 MB eMail Account Now.

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]