9.3. Building xTree Structures

We can now navigate at will through an xTree structure, but how do we build the structure in the first place?

We could build up the structure “by hand” by using xTree, xData, and xElement objects.

CD-ROM reference=9007.txt
t = xTree()
t.RootNode = xElement("greeting")
t.CurPos = r.RootNode
t.Down()
t.CurPos.Bottom = xData("Hello World")

This program builds a tree that corresponds to the following XML document.

CD-ROM reference=9008.txt
<greeting>
Hello World
</greeting>

To better see the correspondence between the Python data structure and the XML file, let us add a couple of methods that will allow us to print XML from these structures easily.

You will remember from chapter 5 that Python allows you to take ...

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.