XML technologies / XML / Python & XML / The Document Object Model / DOM explained

(Excerpt from "Python & XML" by Christopher A. Jones & Fred L. Drake, Jr.)

DOM is essentially a hierarchy of node objects. Starting at the document’s root, distinct from the document element, every construct is represented by a node type: element, text, attribute, or other less common node types. Each node has references to child nodes, which may have the same types as their parent. The complete document forms a tree, from its trunk or root to leaf nodes containing text, childless elements, comments, processing instructions, and other constructs. The following figure shows a simple DOM hierarchy with a root element, two children, and their text children. Depending on the parser, an element’s character data usually consists of several text nodes. Contiguous strings of text data become sequences of text nodes.

A simple DOM hierarchy

Figure: A simple DOM hierarchy

DOM represents the entire document as an object hierarchy. Like other nodes, the document can have children; the outermost element is simply a child of the document node. Other children may include comments and processing instructions before or after the document node, appearing in the appropriate order. The document type declaration is also represented as a document child.

The W3C carefully specified DOM independently of programming languages. Each language exposes its interfaces in its own way; each mapping of DOM into the target language’s idioms is called a binding for DOM. The W3C included bindings for Java and ECMAScript in the specification. Python’s official binding comes from the Python-XML-SIG and is documented in the Python Library Reference, part of Python’s standard documentation. DOM reference material is also available in Python DOM API, but the standard documentation is authoritative for this binding.

The DOM specifications provide CORBA IDL modules and Java interfaces but do not require or even recommend the language-specific IDL mappings adopted by the Object Management Group (OMG). Indeed, the W3C’s Java interfaces do not match the IDL-to-Java mapping. For Python, XML-SIG chose a more Python-friendly mapping with some concessions to IDL-to-Python. Since nobody seems to use the IDL-derived binding, this book covers only the Python-oriented DOM binding.

  

 

 

 

back to top