[ ![Python logo](../_static/py.svg) ](https://www.python.org/)

Theme  Auto  Light  Dark

###  [ Table of Contents ](../contents.html)

  * ` xml.dom  ` â The Document Object Model API 
    * Module Contents 
    * Objects in the DOM 
      * DOMImplementation Objects 
      * Node Objects 
      * NodeList Objects 
      * DocumentType Objects 
      * Document Objects 
      * Element Objects 
      * Attr Objects 
      * NamedNodeMap Objects 
      * Comment Objects 
      * Text and CDATASection Objects 
      * ProcessingInstruction Objects 
      * Exceptions 
    * Conformance 
      * Type Mapping 
      * Accessor Methods 

####  Previous topic

[ ` xml.etree.ElementTree  ` â The ElementTree XML API
](xml.etree.elementtree.html "previous chapter")

####  Next topic

[ ` xml.dom.minidom  ` â Minimal DOM implementation ](xml.dom.minidom.html
"next chapter")

###  This Page

  * [ Report a Bug ](../bugs.html)
  * [ Show Source ](https://github.com/python/cpython/blob/main/Doc/library/xml.dom.rst)

###  Navigation

  * [ index ](../genindex.html "General Index")
  * [ modules ](../py-modindex.html "Python Module Index") | 
  * [ next ](xml.dom.minidom.html "xml.dom.minidom â Minimal DOM implementation") | 
  * [ previous ](xml.etree.elementtree.html "xml.etree.ElementTree â The ElementTree XML API") | 
  * ![Python logo](../_static/py.svg)
  * [ Python ](https://www.python.org/) » 
  *   *   * [ 3.12.3 Documentation ](../index.html) » 
  * [ The Python Standard Library ](index.html) » 
  * [ Structured Markup Processing Tools ](markup.html) » 
  * [ ` xml.dom  ` â The Document Object Model API ]()
  * | 
  * Theme  Auto  Light  Dark  | 

#  ` xml.dom  ` â The Document Object Model API  Â¶

**Source code:** [ Lib/xml/dom/__init__.py
](https://github.com/python/cpython/tree/3.12/Lib/xml/dom/__init__.py)

* * *

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.

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 no way to access an XML
reader/parser/Document builder in an implementation-independent way. There is
also no well-defined way to access these methods without an existing `
Document  ` object. In Python, each DOM implementation will provide a function
` getDOMImplementation()  ` . DOM Level 3 adds a Load/Store specification,
which defines an interface to the reader, but this is not yet available in the
Python standard library.

Once you have a DOM document object, you can access the parts of your XM