Parsing and processing
DOM
An API that parses a whole document into a navigable tree in memory, where every node can be reached from any other.
A DOM parse is random access. You can walk up to a parent, jump to a sibling, run an XPath expression across the whole document, modify it and serialize it back. Everything on this site that reports a document's structure works this way.
The cost is memory proportional to the document, typically several times its byte size. That is fine for a feed and unworkable for a multi-gigabyte export, which is the entire reason the streaming APIs exist.
- Defined in
- W3C DOM Level 3 Core
- Category
- Parsing and processing
- Also written
- Document Object Model, tree parsing
Shown, not asserted
This claim is executed on every test run, against the same engines the tools use. If the behaviour changes, the test fails rather than this page quietly becoming wrong.
Random access is the point: this expression reaches across the whole tree at once, which only a materialised tree allows.
document.xml <r><a><b>deep</b></a><c/></r>expression string(//b/ancestor::r/c/preceding-sibling::a/b)What the engine returns
deep
What catches people out
- Memory use is a multiple of document size, not equal to it. Budget several times the bytes.
- The DOM keeps whitespace text nodes unless you asked the parser to drop them, which is why element.firstChild is so often a newline.
- "The DOM" in a browser and the XML DOM are the same idea with different conveniences; XML has no innerHTML.
Where it bites
A definition on its own is trivia. These are the pages where this term decides whether a document works.
- ToolsXPath testerRuns expressions against a materialised tree, which is what makes them possible.
- ToolsXPath explorerWalks the whole tree to map every path in a document.
Related terms
- SAXA streaming API that pushes events at your code — start element, characters, end element — as it reads, without building a tree.
- StAXA streaming API where your code pulls the next event from the parser, rather than the parser pushing events at your code.
- ParserThe component that reads XML text and reports its structure to an application, or reports a fatal error and stops.
- XPathThe expression language for addressing parts of an XML document — the query language inside XSLT, XSD's identity constraints, Schematron and every DOM API.
Get started
Bring order to the XML your team can't afford to ignore.
Create a free account and get a private workspace to search, validate, diff, and monitor your XML feeds, sitemaps, schemas, and vendor integrations.