Skip to main content
XMLDir

Search 309 pages — tools, formats, elements, namespaces, directory, comparisons, XPath, datatypes, glossary, parse errors, schema errors, use cases, blog and product.

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.

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.