11.5. Navigating a DOM Tree

In this example, which uses the same test.xml input file as the previous example, we print the data content of an XML document by traversing the tree, isolating the text nodes, and printing their contents.

CD-ROM reference=11003.txt
C>type dom2.py """ Print text by navigating DOM nodes. """ # Import the core DOM module. from xml.dom import core # Import the DOM SAX builder. from xml.dom.sax_builder import SaxBuilder # Import the SAX modules. from xml.sax import saxexts,saxlib import sys def PrintText(nodelist): # Print the data content of a node list. for n in nodelist: if n.nodeType == core.TEXT_NODE: # For text nodes, data content is available as # the nodeValue instance variable. print n.nodeValue, elif n.nodeType ...

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.