Main Page

Hierarchy of nodes

The Document Object Model (DOM) is a tree-based API for XML. Its main focus isn’t just to parse XML
code, but rather to represent that code using a series of interlinked objects that can be modified and
accessed directly without reparsing the code.
Using the DOM, code is parsed once to create a tree model; sometimes a SAX parser is used to accom-
plish this. After that initial parse, the XML is fully represented in a DOM model, and the original code is
no longer needed. Although the DOM is slower than SAX and requires more overhead because it creates
so many objects, it is the method favored by Web browsers and JavaScript for its ease of use.
Hierarchy of nodes
So what exactly is a tree-based API? When talking about DOM trees (which are called
), you
are really talking about a hierarchy of
. The DOM defines the
interface as well as a large num-
ber of node types to represent the multiple aspects of XML code:
— The very top-level node to which all other nodes are attached
— The object representation of a DTD reference using the syntax
such as
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
. It can-
not contain child nodes.
— Can be used like a
to hold other nodes
— Represents the contents of a start tag and end tag, such as
. This node type is the only one that can contain attributes as well as child nodes.
— Represents an attribute name-value pair. This node type cannot have child nodes.
— Represents plain text in an XML document contained within start and end tags or
inside of a CData Section. This node type cannot have child nodes.
— The object representation of
<![CDATA[ ]]>
. This node type can have only
text nodes as child nodes.
— Represents an entity definition in a DTD, such as
<!ENTITY foo “foo”>
. This
node type cannot have child nodes.
— Represents an entity reference, such as
. This node type cannot
have child nodes.
— Represents a PI. This node type cannot have child nodes.
— Represents an XML comment. This node type cannot have child nodes.
— Represents notation defined in a DTD. This is rarely used and so won’t be
included in this discussion.
Note that the DOM is a language-independent API, meaning that it is not tied to
Java, JavaScript, or any other language for implementation. For the purposes of this
book, however, I place most focus on the JavaScript implementation.
DOM Basics
09_579088 ch06.qxd 3/28/05 11:37 AM Page 163

JavaScript EditorFree JavaScript Editor     Ajax Editor