12.8. Node Lists

Pyxie provides a collection of functions for the creation and processing of lists of nodes. The Descendants method creates a Python list of all the nodes beneath the active position in an xTree. The following program creates a list of descendants of the root node and uses it to print the data content of an XML document backwards.

CD-ROM reference=12018.txt
C>type p16.py

"""
Print the data content of an XML
document backwards.
"""

from pyxie import *

import sys
t = File2xTree (sys.argv[1])
L = t.Descendants()
L.reverse()
for node in L:
      if isinstance(node,xData):
            strlist = list(node.Data)
            strlist.reverse()
            print string.join(strlist,"")

C>python p16.py hello.xml

dlroW
 olleH

C>type AbleWasIEreISawElba.xml <palindrome> able was I ...

Get XML Processing with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.