XML DOM Nodes
Everything in an XML document is a node.
Nodes
According to the DOM, everything in an XML document is a node.
The DOM says that:
- The
entire document is a document node
- Every XML tag is an element node
- The texts contained in the XML elements are text
nodes
- Every XML attribute is an attribute node
- Comments are comment nodes
Node Hierarchy
Nodes have a hierarchical relationship to each other.
All nodes in an XML document form a document tree (or node tree). Each
element, attribute, text, etc. in the XML document represents a node in the
tree. The tree starts at
the document node and continues to branch out until it has reached all
text nodes at the lowest level of the tree.
The terms "parent" and "child" are used to describe the relationships between
nodes. Some nodes may have child nodes, while other nodes do not have children
(leaf nodes).
Because the XML data is structured in a tree form, it can be traversed without
knowing the exact structure of the tree
and without knowing the type of data contained within.
DOM Node Hierarchy Example
Look at the following XML file: books.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
|
Notice that the root element in the XML document above is named <bookstore>. All other elements in the
document
are contained within <bookstore>.
The <bookstore> element represents the root node of the DOM tree.
The root node <bookstore> holds four <book> child nodes.
The first <book> child node also holds four children: <title>, <author>, <year>, and
<price>, which contains one text node each, "Everyday Italian", "Giada
De Laurentiis", "2005", and
"30.00".
IMPORTANT! Text is
always stored in text nodes. A common error in DOM processing is
to navigate to an element node and expect it to contain the text. However, even the simplest element node has a text node under
it. For example, in <year>2005</year>, there is an element node (year), and a
text node under it, which contains the text (2005).
The following image illustrates a fragment of the DOM node tree from the XML document above:
For a complete reference of all the node types and which children they may
have, go to our Node types reference.
|