XML DOM Node Tree
XML DOM views an XML document as a tree of nodes.
All nodes in the tree have relationships with each other.
XML DOM Node Tree
XML DOM views an XML document as a tree structure. This tree structure is known as the node tree.
All nodes in this tree can be accessed. You can modify or delete their content, and create new elements.
This node tree shows the collection of nodes and their relationships. The tree starts with the root node and branches out to text nodes at the lowest level:
The image above represents the XML file books.xml.
Parent, Child, and Sibling Nodes
Nodes in the node tree have a hierarchical relationship with each other.
Parent, child, and sibling nodes describe these relationships. A parent node has child nodes, and child nodes at the same level are called sibling nodes (brothers or sisters).
- The top node in the node tree is called the root node
- Each node (except the root) has exactly one parent node
- Nodes can have any number of child nodes
- Leaf nodes are nodes without children
- Sibling nodes are nodes with the same parent
The image below shows a part of the node tree and the relationships between the nodes:
Since XML data is structured in a tree format, it can be traversed without knowing the exact structure of the tree or the types of data it contains.
You will learn more about traversing the node tree in later sections of this tutorial.
First Child - Last Child
Consider the following XML snippet:
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
</bookstore>
In the XML above, the <title>
element is the first child of the <book>
element, and the <price>
element is the last child of the <book>
element.
Additionally, the <book>
element is the parent of the <title>
, <author>
, <year>
, and <price>
elements.