Python xml.dom: access an XML document as a tree of nodes
Answer: The xml.dom package presents an XML document as a tree of objects, which is useful when you need random access rather than a one-pass SAX view.
Trees versus events
A DOM implementation shows an XML document as a tree, or lets client code build that tree from scratch.
Callers then walk the structure through a set of objects with well-known interfaces.
The DOM is extremely useful for random-access applications.
SAX only lets you see one bit of the document at a time, so a text node gives you no access to its containing element.
You can assemble your own tree from SAX events, but the DOM saves you from writing that code.
The Python mapping
The Document Object Model is defined by the W3C in stages they call levels.
The Python mapping of the API is substantially based on the DOM Level 2 recommendation.
xml.dom.minidom follows the DOM Level 1 specification.
Applications typically start by parsing some XML into a DOM.
xml.dom contains registerDOMImplementation and getDOMImplementation. EMPTY_NAMESPACE indicates that no namespace is associated with a node.
See also
Claims
- id: C1 claim: A DOM implementation presents an XML document as a tree structure. source: S1 evidence: "A DOM implementation presents an XML document as a tree structure"
- id: C2 claim: The Python mapping is substantially based on the DOM Level 2 recommendation. source: S2 evidence: "substantially based on the DOM Level 2 recommendation"
- id: C3 claim: The model is extremely useful for random-access applications. source: S1 evidence: "The DOM is extremely useful for random-access applications"
Sources
- [S1] xml.dom: The Document Object Model API. Python Software Foundation. https://docs.python.org/3/library/xml.dom.html . License: PSF License Version 2. Accessed 2026-11-26.
- [S2] xml.dom documentation (source). CPython. https://raw.githubusercontent.com/python/cpython/main/Doc/library/xml.dom.rst . License: PSF License Version 2. Accessed 2026-11-26.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, xml.dom: The Document Object Model API; CPython, xml.dom documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, xml.dom: The Document Object Model API; CPython, xml.dom documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.
Cite this
iinventedlight. 2026. "Python xml.dom: access an XML document as a tree of nodes." iinventedlight Library, iil:note:kv2syyds, version of 2026-11-26. https://iinventedlight.com/library/notes/python-xml-dom/. License: CC-BY-4.0.
@misc{iil_note_kv2syyds,
author = {{iinventedlight}},
title = {{Python xml.dom: access an XML document as a tree of nodes}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-26},
url = {https://iinventedlight.com/library/notes/python-xml-dom/},
note = {Permanent ID iil:note:kv2syyds. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- python-xml-dom.schema.json (schema; CC-BY-4.0)
- python-xml-dom.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-xml-dom.md.