13.5 xml.dom -- The Document Object Model API

New in version 2.0.

The Document Object Model, or ``DOM,'' is a cross-language API from the World Wide Web Consortium (W3C) for accessing and modifying XML documents. A DOM implementation presents an XML document as a tree structure, or allows client code to build such a structure from scratch. It then gives access to the structure through a set of objects which provided well-known interfaces.

The DOM is extremely useful for random-access applications. SAX only allows you a view of one bit of the document at a time. If you are looking at one SAX element, you have no access to another. If you are looking at a text node, you have no access to a containing element. When you write a SAX application, you need to keep track of your program's position in the document somewhere in your own code. SAX does not do it for you. Also, if you need to look ahead in the XML document, you are just out of luck.

Some applications are simply impossible in an event driven model with no access to a tree. Of course you could build some sort of tree yourself in SAX events, but the DOM allows you to avoid writing that code. The DOM is a standard tree representation for XML data.

The Document Object Model is being defined by the W3C in stages, or ``levels'' in their terminology. The Python mapping of the API is substantially based on the DOM Level 2 recommendation. Some aspects of the API will only become available in Python 2.1, or may only be available in particular DOM implementations.

DOM applications typically start by parsing some XML into a DOM. How this is accomplished is not covered at all by DOM Level 1, and Level 2 provides only limited improvements. There is a DOMImplementation object class which provides access to Document creation methods, but these methods were only added in DOM Level 2 and were not implemented in time for Python 2.0. There is also no well-defined way to access these methods without an existing Document object. For Python 2.0, consult the documentation for each particular DOM implementation to determine the bootstrap procedure needed to create and initialize Document and DocumentType instances.

Once you have a DOM document object, you can access the parts of your XML document through its properties and methods. These properties are defined in the DOM specification; this portion of the reference manual describes the interpretation of the specification in Python.

The specification provided by the W3C defines the DOM API for Java, ECMAScript, and OMG IDL. The Python mapping defined here is based in large part on the IDL version of the specification, but strict compliance is not required (though implementations are free to support the strict mapping from IDL). See section 13.5.3, ``Conformance,'' for a detailed discussion of mapping requirements.

See Also:

Document Object Model (DOM) Level 2 Specification
The W3C recommendation upon which the Python DOM API is based.
Document Object Model (DOM) Level 1 Specification
The W3C recommendation for the DOM supported by xml.dom.minidom.
Users that require a full-featured implementation of DOM should use the PyXML package.
CORBA Scripting with Python
This specifies the mapping from OMG IDL to Python.

See About this document... for information on suggesting changes.