9.9. Some Utility Bits and Pieces

9.9.1. Descendants

In order to implement the --descendant command-line switch, we need to be able to find a list of all descendant nodes. This is a classic example of a recursive function that occurs very often in XML tree processing.

In the code below, notice that the auxillary method Descendants1 does most of the real work. The Descendants function serves to initialize the result list, which is passed into Descendants1 and then returned.

 CD-ROM reference=9029.txt class xTree: def Descendants1(self,res,n): """ Add descendants of node "n" to the result list "res." This is an internal recursive method invoked from the Descendants method. """ if n is None: return pos = n.Down if pos is None: return res.append ...

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.