XML technologies / XML / XML in a Nutshell / The Document Object Model (DOM) / DOM basics

(Excerpt from “XML in a Nutshell” by Elliotte Rusty Harold & W. Scott Means)

DOM centers on abstract interfaces. Implementations use their own objects to support interfaces defined in the specification. These interfaces are divided into modules, allowing implementations to support only parts rather than the entire specification. XML parsers, for example, need not support HTML-specific parts. Modularization provides a simple mechanism for developers to determine which DOM parts a particular implementation supports.

Successive DOM versions are called levels. Level 1, the W3C’s first release, focused on HTML and XML in browsers, supporting dynamic HTML and basic XML processing. It described only an object structure and its manipulation, assuming documents already existed in the browser environment. It did not specify loading documents into that structure or recreating documents from it.

Later levels added functionality. Level 2, published as one specification per module, revised Level 1’s Core and HTML modules and added Views, Events, Style, Traversal and Range. Level 3 added abstract schemas, loading, saving and XPath, and revised Core and Events.

Other W3C specifications define DOM extensions for their own needs. Mathematical Markup Language (MathML), Scalable Vector Graphics (SVG), Synchronized Multimedia Integration Language (SMIL) and SMIL Animation all define DOMs giving access to their vocabularies’ details.

Note: For a complete overview of module requirements, see Document Object Model (DOM) Requirements. A list of all specifications, including those under development, is at Document Object Model (DOM) Technical Reports. DOM is also incorporated by reference into many other specifications, particularly the Java API for XML processing (JAXP).

Developers using DOM for XML typically base their work on the Core module.

DOM notation

One goal of the Document Object Model is operating-system and language neutrality. Its interfaces are therefore written in the Object Management Group’s Interface Description Language (IDL). Here and in the DOM reference, we use IDL terminology to match the specification’s notation. Thus “attribute” means roughly a C++ or Java instance variable, not an XML attribute—the key/value pair in an element’s start-tag.

Language-independent IDL must be mapped to the target programming language using rules also specified by OMG. Consider this interface:

interface NodeList {
  Node               item(in unsigned long index);
  readonly attribute unsigned long    length;
};

In Java, it would look like this:

package org.w3c.dom;

   public interface NodeList {
       public Node item(int index);

       public int getLength(  );

   }

In ECMAScript, we would obtain, for example:

Object NodeList
   Das Objekt NodeList hat die folgenden Properties:
     length
       Dieser nur zum Lesen geeignete Wert ist vom Typ Zahl.
   Das Objekt NodeList hat die folgenden Methoden:
     item(index)
       Das Ergebnis dieser Methode ist ein Objekt vom Typ Node.
       Der Parameter index ist vom Typ Zahl.
       Anmerkung: Zur Dereferenzierung dieses Objekts kann man auch die Notation mit
       den eckigen Klammern verwenden, z.B. obj[1]). Dereferenzierung mit einer
       ganzen Zahl"index" ist identisch mit dem Aufruf der Methode item mit demselben
       "index".

The tables here reproduce DOM’s IDL information, including available functionality and when it became available. Implementations interpret these functions differently, so consult your implementation’s documentation to learn how it maps standard interfaces to your language.

DOM strengths and weaknesses

Like other tools, DOM suits some problems better than others. Its hierarchy links document parts by references, so the whole document must be read and parsed before an application can use it. The entire document resides in RAM, potentially causing a significant slowdown if memory runs short. Early implementations used many times the file size in memory. This makes DOM unsuitable for huge documents or applications better served by incremental reading before parsing is complete.

Nevertheless, DOM is among the most mature and best-supported technologies for applications needing random access to different document parts at different times, or simultaneous changes to XML structure.

  

 

 

 

back to top