A document structure

This section explains how a typical document may be represented using the DOM hierarchy of nodes. Some of this material is adapted from section 1.1.1 of the DOM (Core) Level 1 specification.

Document nodes

The top level node of a document is a Document (DOMDocument) object. A Document object can have child nodes of the following types:

For this discussion we'll consider in detail only the DOMElement node. The other nodes can contain important information, but none of them have child nodes of their own, and generally most of a document's structure is contained in its elements.

Element nodes

Document can have only one child node of type DOMElement. This node corresponds to the top-level element in the document (of which there is only one instance), and it is the starting point for any further navigations and manipulations of the document.

A DOMElement node can have the following child nodes, in any number and order:

These objects correspond to the HTML constructs that you typically find inside an element: sub-elements, text, comments, processing instructions, and entity references.

Navigating the node structure

Since DOMElement inherits the properties and methods of DOMNode, you can make use of DOMNode's properties for navigating in the node structure: childNodes, firstChild, lastChild, nextSibling, parentNode, and previousSibling. Using these operations, you can traverse the entire document.

Attribute nodes

One markup component that is missing from this discussion so far is the attribute. Even though attributes correspond to a DOM interface (DOMAttr), they do not appear as child nodes of DOMElement. Rather, a DOMAttr is accessed via the getAttributeNode method of DOMElement, which returns a DOMAttr object. (DOMElement can also access an attribute value as a string via the getAttribute method.)


Right arrow
Next Topic
Left arrow
Previous Topic
Table of contents
Table of Contents

Copyright © SoftQuad Software Inc. 1999