The DOM is a specification for an API that lets programmers manipulates XML held in memory. The
DOM specification is language-independent, and bindings are available for many programming lan-
guages, including C++. XmlDocument is based upon the DOM, with Microsoft extensions. Because it
works with XML in memory, it has several advantages and disadvantages over the XmlReader forward-
only approach.
One advantage is that, in reading the entire document and building a tree in memory, you have access to all
the elements and can wander through the document at will. You can also edit the document, changing,
adding, or deleting nodes, and write the changed document back to disk again. It is even possible to create
an entire XML document from scratch in memory and write it out—serialize it—and this is a useful alter-
native to using XmlWriter.
The main disadvantage is that the whole of an XML document is held in memory at once, so the amount
of memory needed by your program is going to be proportional to the size of the XML document you’re
working with. This means that if you’re working with a very large XML document—or have limited
memory—you might not be able to use XmlDocument. Another disadvantage is that the API for the
XmlDocument is specified through the DOM model, limiting the options for implementing performance
improvements behind the XmlDocument class.
XML Document Loaded in a DOM Tree
Working with an XmlDocument class is fundamentally different from working with the XmlReader and
XmlWriter classes, although they share some similarities, such as the concept of nodes. When you use
DOM processing, the entire XML document is loaded into a tree structure that matches the hierarchical
structure of the document. The DOM provides a set of functions that examine the tree structure and
manipulate the document’s content. To see an example of this, consider the XML document shown in
Listing 6-1 that contains a simple book store along with information about the various books that are
part of the bookstore.
Listing 6-1: XML Document That Represents the Bookstore
<?xml version=’1.0’?>
<!– This file represents a fragment of a book store inventory database –>
<bookstore>
<book genre=”autobiography”>
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name></author>
<price>8.99</price>
</book>
<book genre=”novel”>
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre=”philosophy”>
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>
Note that the books.xml file shown in Listing 6-1 is used throughout all the examples presented in this
chapter. An XML document has a tree structure under the DOM, and each object in the tree is a node. T
document, modeled using the DOM, would be represented as the tree structure shown in Figure 6-1.

domtree