Home > Articles

  • Print
  • + Share This
From the author of

DOM Basics

Developers who don't want to write their own low-level code for manipulating XML documents may choose from among several APIs that make working with XML a bit easier. The World Wide Web Consortium's DOM is one of the most mature, robust, and feature-rich APIs available. It's a model in the best sense of the word, because it describes a language-neutral object model that can be (and has been) implemented in several programming languages. In addition, several API implementations offer extended functionality that can save programmers additional development effort.

Although the DOM is a fairly rich model, at its core it's simple to understand. The DOM specifies a set of interfaces (or classes, if you prefer); methods; and properties that deal with a document (usually XML or HTML) as a tree. The most important of these interfaces is the Node, and from it are derived most of the other major interfaces. Figure 1 shows an inheritance diagram for the Node interface.

Figure 1Figure 1 DOM Node interface.

The Document interface is used to manage the root node of an XML document loaded into the DOM. The XML declaration and the Document element (sometimes called the root element of the document) are both children of the Document. The Document element may have other elements as children, and these in turn may have other children. The lowest level of the DOM tree is occupied by empty elements or text nodes. Any of the element nodes in the tree may have attributes. If we consider an element to be roughly equivalent to an object in an object-oriented programming language, an attribute is similar to a property or attribute of an object. (I think this is a useful analogy, but don't want to stretch the point too far because the DOM also defines an Attribute interface with associated methods and properties!) Figure 2 shows a simple XML document as a DOM tree.

Figure 2Figure 2 XML document as a DOM tree.

A small subset of the DOM interfaces and methods will probably handle most of what you need to do when adding XML support to an existing application. But before we discuss them, let's first look at some general considerations for using the DOM with C++ and MSXML.

  • + Share This
  • 🔖 Save To Your Account