About the Document Object Model

The Document Object Model (DOM) describes the content of XML data in the form of a document object, which contains other objects that describe the various data elements of the XML document. The DOM also specifies an interface for interacting with the objects in the model. This is the interface exposed to the IDL user.

Note
For more information on XML, see About XML.

When to Use the DOM

There are two basic types of parsers for XML data: object-based and event-based. The DOM is object-based and as such has advantages in certain situations over an event-based parser such as SAX. In general, use the DOM:

For more information on the difference between the two parsers, see About XML Parsers.

About the DOM Structure

Here is an example of an XML file that is used in an application to define a weather-monitoring plug-in component:

<?xml version="1.0" encoding="UTF-8"?> 
<plugin type="tab-iframe"> 
   <name>Weather.com Radar Image [DEN]</name> 
   <description>600 mile Doppler radar image for DEN</description> 
   <version>1.0</version> 
   <tab> 
      <icon>weather.gif</icon> 
      <tooltip>DEN Doppler radar image</tooltip> 
   </tab> 
</plugin> 

The contents of this file constitute an XML document. When you want to work with this data, you can use IDL to load the file, parse it, and store it in memory in DOM format. The sample file listed above is stored in the DOM structure as shown in Figure 21-1.

Figure 21-1: XML DOM Tree Structure: Plug-in Example

About_the_Document_Object_Model-1.jpg

The DOM structure is a tree of nodes, where each node is represented as a box in the figure. The type of each node is in boldface. The contents of the node are in normal type.

Note that whitespace and newline characters can appear in this tree as text nodes, but are omitted in this picture for clarity. It is important to keep this in mind when exploring the DOM tree. There are parsing options available that can prevent the creation of ignorable-whitespace nodes (see Working with Whitespace).

The attribute node (Attr) is not actually a child of the element node, but is still associated with it, as indicated by the dotted line.

How IDL Uses the DOM Structure

To access the XML data in the structure, you need to create a set of IDL objects that correspond to the portion of the DOM tree in which you are interested. You use the following process to create the DOM tree and the corresponding IDL objects:

  1. Create an IDLffXMLDOMDocument object.
  2. Load the XML file. This step parses the XML data from the file and creates the DOM tree in memory.
  3. Use the IDLffXMLDOMDocument object to create IDLffXMLDOM objects that essentially mirror portions of the DOM tree, as shown in Figure 21-2.

You then use the IDLffXMLDOM objects to access the actual XML data contained in the DOM tree.

Figure 21-2: The DOM and IDL Trees

About_the_Document_Object_Model-2.jpg

The creation and destruction of the IDL objects do not alter the DOM structure. There are explicit methods for modifying the DOM structure. The IDL objects are merely access objects that are used to manipulate the DOM tree nodes.