Introduction to the DOM
The Document Object Model (DOM) presents an easily processed standardized interpretation of an XML document to applications and scripts. The DOM implementation in Microsoft® XML Core Services (MSXML) allows you to load or create a document; gather errors, if any; access and manipulate the information and structures contained within the document; and save the document back out to an XML file, if necessary.
DOM and MSXML
The DOM implementation is just one part of the MSXML parser. The following diagram shows the tasks involved in parsing an XML document and presenting the information to an application or script.

For another approach to XML document parsing, the MSXML parser also provides a different application programming interface, Simple API for XML (SAX2).

The DOM approach creates an object tree that is managed by the MSXML parser. This allows developers to take advantage of the logic built into MSXML for managing XML content, rather than having to create their own. Developers who need to build applications for specific purposes may find it more efficient to use SAX2 to read the document and then manage the information in their internal data structures.
For more information about SAX2, see the SAX2 Developer's Guide.
Modeling Documents as Node Trees
The DOM provides you with an interface for loading, accessing, manipulating, and serializing XML documents. The DOM provides a representation of a complete XML document stored in memory, providing random access to the contents of the entire document. The DOM allows applications to rely on the logic provided by the MSXML parser to handle XML-based information, using its facilities rather than writing custom code to read and process XML.
When the MSXML parser loads an XML document into a DOM, it reads it from start to finish and creates a logical model of nodes from the structures and content within the XML document. The document itself is considered a single node that contains all of the other nodes, including a node representing the root element, which, in turn, contains all of the element, attribute, and text nodes in the document. The following XML document has a simple multi-tier structure.
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="show_book.xsl"?>
<!DOCTYPE catalog [
<!NOTATION XLS PUBLIC "http://www.microsoft.com/office/excel/">
<!ELEMENT COLLECTION (DATE? , BOOK+) >
<!ATTLIST COLLECTION
xmlns:dt CDATA #FIXED "urn:schemas-microsoft-com:datatypes">
<!ELEMENT BOOK(TITLE, AUTHOR, PUBLISHER) >
<!ELEMENT DATE (#PCDATA) >
<!ELEMENT TITLE (#PCDATA) >
<!ELEMENT AUTHOR (#PCDATA) >
<!ELEMENT PUBLISHER (#PCDATA) >
]>
<!--catalog last updated 2000-11-01-->
<catalog xmlns="">
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description><![CDATA[An in-depth look at creating applications with XML, using <, >,]]> and &.</description>
</book>
<book id="bk109">
<author>Kress, Peter</author>
<title>Paradox Lost</title>
<genre>Science Fiction</genre>
<price>6.95</price>
<publish_date>2000-11-02</publish_date>
<description>After an inadvertant trip through a Heisenberg Uncertainty Device, James Salway discovers the problems of being quantum.</description>
</book>
</catalog>
After MSXML parsing, the top two levels of the node structure representing this document will look like this.

The topmost node is the document itself, which contains all of the other nodes. Immediately within the document are nodes representing the XML declaration, the style sheet processing instruction, the DOCTYPE declaration, and the root element for the document, in this case, catalog.
The catalog element contains the real content of the document, and its structure is shown below.

This part of the DOM contains element, attribute, text, and CDATA nodes. (The character references and built-in entities are converted to ordinary text by the parser, but the CDATA section has its own node.)
Working with the DOM
The DOM allows applications to work with XML document structures and information as program structures rather than text streams. Applications and scripts can read and manipulate these structures without knowing the details of XML syntax, taking advantage of the facilities built into the DOM API of MSXML.
The DOM uses two key abstractions: a tree-like hierarchy and nodes that represent document content and structures. The hierarchy is composed of these nodes, which may contain or be contained by other nodes. For developers, this means that much of the work of XML processing requires navigating this tree structure to find or modify the information it contains. Working with XML requires thinking of information in terms of nested containers, and making sure that information is put into or retrieved from the right container.
The DOM treats nodes as generic objects, making it possible to create a script that loads a document and then traverses all of the nodes, reporting what it finds in the tree.
The following are exposed by the XML DOM.
The DOM programming interfaces enable applications to traverse the tree and manipulate its nodes. Each node is defined as a specific node type, according to the XML DOM enumerated constants, which also define valid parent and child nodes for each node type. For most XML documents, the most common node types are element, attribute, and text. Attributes occupy a special place in the model because they are not considered child nodes of a parent, and are treated more like properties of elements. An additional programming interface, the IXMLDOMNamedNodeMap, is provided for attributes.
This sample Active Server Pages (ASP) script uses the MSXML parser to parse a document into a DOM tree, then move down the tree from the root node and report the kinds of nodes it encounters and their content.
The first version uses Microsoft Visual Basic® Scripting Edition (VBScript) to load the document and walk the tree. If no form input is provided, it presents the user with a form to gather the URL of an XML document. The user then submits that back to the script, which parses the document and presents a tree.
<%@LANGUAGE=VBScript%>
<html>
<head>
<title>Tree walk test - VBScript</title>
</head><body>
<%
function attribute_walk(node)
For i=1 to indent
Response.Write(" ")
Next
For Each attrib In node.attributes
Response.Write("|--")
Response.Write(attrib.nodeTypeString)
Response.Write(":")
Response.Write(attrib.name)
Response.Write("--")
Response.Write(attrib.nodeValue)
Response.Write("<br />")
Next
end function
function tree_walk(node)
dim nodeName
indent=indent+2
For Each child In node.childNodes
For i=1 to indent
Response.Write(" ")
Next
Response.Write("|--")
Response.Write(child.nodeTypeString)
Response.Write("--")
If child.nodeType<3 Then
Response.Write(child.nodeName)
Response.Write("<br />")
End If
If (child.nodeType=1) Then
If (child.attributes.length>0) Then
indent=indent+2
attribute_walk(child)
indent=indent-2
End If
End If
If (child.hasChildNodes) Then
tree_walk(child)
Else
Response.Write child.text
Response.Write("<br />")
End If
Next
indent=indent-2
end function
xmlFile=Request.Form("fileURI")
Dim root
Dim xmlDoc
Dim child
Dim indent
indent=0
Set xmlDoc = CreateObject("Msxml2.DOMDocument.4.0")
xmlDoc.async = False
xmlDoc.validateOnParse=False
xmlDoc.load(xmlFile)
If xmlDoc.parseError.errorcode = 0 Then
'Walk from the root to each of its child nodes:
Response.Write("<pre>")
tree_walk(xmlDoc)
Response.Write("</pre>")
Else
%>
<h1>XML Parsing - DOM Tree Walk Demo</h1>
<form id="location" method="post" action="">
<input type="text" name="fileURI" maxlength="255" size="20" id="XMLurl" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
<% End If%>
</body></html>
At the bottom, the script contains a main routine that either loads a document and passes it to the tree walker or presents a form asking which document to load. This script relies on the tree_walk function, a recursive function that moves from node to node in the tree and presents a suitably formatted version of the contents. That function in turn relies on an attribute_walk function to present attribute content, because attribute nodes are not considered children of element nodes within the DOM.
The Microsoft JScript® version is similar; however, it requires extra lines of code to avoid overwriting variables during the recursive tree walking.
<%@LANGUAGE=JScript%>
<html>
<head>
<title>Tree walk test - JScript</title>
</head><body>
<%
function attribute_walk(node) {
for (k=1; k<indent; k++) {
Response.Write(" ");
}
for (m=0; m<node.attributes.length; m++){
attrib = node.attributes.item(m);
Response.Write("|--");
Response.Write(attrib.nodeTypeString);
Response.Write(":");
Response.Write(attrib.name);
Response.Write("--");
Response.Write(attrib.nodeValue);
Response.Write("<br />");
}
} //end attribute_walk
function tree_walk(node) {
indent=indent+2;
for (current=0; current<node.childNodes.length; current++) {
child=node.childNodes.item(current);
for (j=1; j<indent; j++){
Response.Write(" ");
}
Response.Write("|--");
Response.Write(child.nodeTypeString);
Response.Write("--");
if (child.nodeType<3) {
Response.Write(child.nodeName);
Response.Write("<br />");
}
if (child.nodeType==1) {
if (child.attributes.length>0) {
indent=indent+2;
attribute_walk(child);
indent=indent-2;
}
}
if (child.hasChildNodes) {
//store information so recursion is possible
depthList[depth]=current;
depth=depth+1;
tree_walk(child);
//return from recursion
depth=depth-1;
current=depthList[depth];
}else{
Response.Write (child.text);
Response.Write("<br />");
}
}
indent=indent-2;
}
//recursion-tracking variables
depth=0;
depthList=new Array();
indent=0;
xmlFile=new String();
xmlFile=Request.Form("fileURI");
xmlFile=""+xmlFile; //makes string clean for passing to MSXML
xmlPresented=false;
var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.4.0");
xmlDoc.async = false;
xmlDoc.validateOnParse=false;
//xmlFile="http://127.0.0.1/ms/local.xml"
if ((xmlFile)) {
xmlDoc.load(xmlFile);
if (xmlDoc.parseError.errorcode == null) {
Response.Write("<pre>");
tree_walk(xmlDoc);
Response.Write("</pre>");
xmlPresented==true;
}
}
if (xmlPresented==false){
%>
<h1>XML Parsing - DOM Tree Walk Demo</h1>
<form id="location" method="post" action="">
<input type="text" name="fileURI" maxlength="255" size="20" id="fileURI" />
<br />
<input type="submit" name="submit" value="submit" />
</form>
<% } %>
</body></html>
Additional MSXML Features
The World Wide Web Consortium (W3C) Document Object Model Level 1 Specification defines two groups of DOM programming interfaces.
- Fundamental
- The W3C fundamental interfaces include those required to write applications that manipulate XML documents.
- Extended
- The W3C extended interfaces include those that make programming more convenient for developers.
The MSXML DOM implements both fundamental and extended interfaces and also provides additional methods to support XSL Transformations (XSLT), XPath, namespaces, and data types. This approach allows developers to work with a single consistent API for document processing and transformations.
For example, the selectNodes method supports XPath syntax to enable sophisticated queries for nodes within a particular context or subtree of the overall tree structure. The transformNode method supports the use of XSLT to perform transformations.
See Also
IXMLDOMNamedNodeMap | selectNodes Method | transformNode Method | XML DOM Enumerated Constants | Template-Driven Transformations | Using XPath Expressions to Select Nodes | Using Namespaces in Documents