9.2. Navigating a Tree

When doing tree navigation, it is useful to be able to keep track of a current position in a tree. A sensible place to put that functionality would be in a tree object—that is, implement the navigation functionality as methods of a tree class. We will create a class xTree to act as the top-level XML document object.

CD-ROM reference=9005.txt
class xTree:
def __init__(self):
self.RootNode = None
self.CurPos = None

In the constructor above, we have allocated storage for RootNode, which references the root node in the tree. We have also allocated storage for CurPos—the current position in the tree. When a tree is created, CurPos starts out being the same as RootNode, but it can be moved from node to node with the following ...

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.