11.7. Accessing Attributes

Attributes in the DOM are accessed from an element node by means of the attributes variable. Attributes are stored in a structure known as a NamedNodeMap. A NamedNodeMap is like a NodeList except that nodes in a NamedNodeMap can be retrieved, based on the value of their nodeName attribute.

In the following example, a DOM tree is traversed, and attribute names and values printed out by traversal of a NamedNodeMap.

CD-ROM reference=11006.txt
C>type dom4.py """ Printing attributes from a DOM tree """ from xml.dom import core from xml.dom.walker import Walker from xml.dom.sax_builder import SaxBuilder import string,sys from xml.sax import saxexts,saxlib p = saxexts.make_parser() dh = SaxBuilder() p.setDocumentHandler(dh) ...

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.