When Should I Use DOM?

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - SAX2 Developer's Guide

When Should I Use DOM?

The following are some suggestions for when to use DOM.

When you need random access to document data

If random access to information is crucial, it is better to use the DOM to create a tree structure for the data in memory. When you use SAX, the document is not in memory. Therefore, you must handle data in the order in which it is processed. SAX can be difficult to use when the document contains many internal cross-references, such as ID and IDREF attributes. DOM stores the document in memory and allows you to access its parts without reading the entire document.

When you want to implement complex searches

It is your responsibility to maintain data structures holding context information that you must retain, such as the attributes of the current element's ancestor.

No SAX implementation in current browsers

SAX support is not built into Microsoft® Internet Explorer.

When you need to perform XSLT transformations

The DOM works better for XSL Transformations (XSLT) where the source XML document is transformed based on the XSLT template applied. For example, to create multiple views of the same data, you must transform it using one of two style sheets. In order for this transformation to take place, you must also create two instances of the DOM. One stores the XML source; the other stores the transformed content.

When you need complex XPath filtering

Use the DOM if you must perform complex XML Path Language (XPath) filtering and retain complex data structures that hold context information. The tree structure of the DOM retains context information automatically. With SAX, you must retain the context information yourself.

When you want to modify and save XML

The DOM allows you to create or modify a document in memory, as well as read a document from an XML source file. SAX is designed for reading, not writing, XML documents. The DOM is the better choice for modifying an XML document and saving the changed document to memory.