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
documents
), you
are really talking about a hierarchy of
nodes
. The DOM defines the
Node
interface as well as a large num-
ber of node types to represent the multiple aspects of XML code:
?
Document
— The very top-level node to which all other nodes are attached
?
DocumentType
— The object representation of a DTD reference using the syntax
<!DOCTYPE >
,
such as
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
. It can-
not contain child nodes.
?
DocumentFragment
— Can be used like a
Document
to hold other nodes
?
Element
— Represents the contents of a start tag and end tag, such as
<tag></tag>
or
<tag/>
. This node type is the only one that can contain attributes as well as child nodes.
?
Attr
— Represents an attribute name-value pair. This node type cannot have child nodes.
?
Text
— 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.
?
CDataSection
— The object representation of
<![CDATA[ ]]>
. This node type can have only
text nodes as child nodes.
?
Entity
— Represents an entity definition in a DTD, such as
<!ENTITY foo “foo”>
. This
node type cannot have child nodes.
?
EntityReference
— Represents an entity reference, such as
&quot;
. This node type cannot
have child nodes.
?
ProcessingInstruction
— Represents a PI. This node type cannot have child nodes.
?
Comment
— Represents an XML comment. This node type cannot have child nodes.
?
Notation
— 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.
163
DOM Basics
09_579088 ch06.qxd 3/28/05 11:37 AM Page 163


JavaScript EditorFree JavaScript Editor     Ajax Editor


©