XML DOM Nodes
- Previous Page DOM Introduction
- Next Page DOM Access Node
According to XML DOM, all content in an XML document isNode:
- The entire document is a document node
- Each XML element is an element node
- The text in an XML element is a text node
- Each attribute is an attribute node
- Comments belong to comment nodes
DOM Example
Please see the following XML file (books.xml):
<?xml version="1.0" encoding="UTF-8"?> <bookstore> <book category="美食"> <title lang="zh">雅舍谈吃</title> <author>梁实秋</author> <press>江苏文艺出版社</press> <year>2013</year> <price>35</price> <ISBN>9787539962771</ISBN> </book> <book category="children"> <title lang="zh">The Fantastic Mr. Fox</title> <author>Rolf Dahl</author> <translator>Da Wei</translator> <press>Tomorrow Publishing House</press> <year>2009</year> <price>10</price> <ISBN>9787533259563</ISBN> </book> <book category="literature"> <title lang="zh">Turning Familiarity into Strangeness</title> <author>Zygmunt Bauman</author> <author>Peter Hafner</author> <translator>Wang Liqiu</translator> <press>Nanjing University Press</press> <year>2023</year> <price>68</price> <ISBN>9787305269387</ISBN> </book> <book category="science"> <title lang="zh">Can You Fly, Like a Bird?</title> <author>Richard Dawkins</author> <author>Yana Renzova</author> <translator>GAO Tianyu</translator> <press>Hunan Science and Technology Press</press> <year>2023</year> <price>88</price> <ISBN>9787571019075</ISBN> </book> <book category="politics" cover="softcover"> <title lang="zh">On the Democratic Spirit of America</title> <author>Tocqueville</author> <translator>Dong Guoliang</translator> <press>Shangwu Yinshe</press> <year>1989</year> <price>60</price> <ISBN>9787100124553</ISBN> </book> </bookstore>
In the above XML, the root node is <bookstore>.
All other nodes in the document are included within the <bookstore>.
The root node <bookstore> has 5 <book> nodes.
The first <book> node contains 6 child nodes: <title>, <author>, <press>, <year>, <price>, and <ISBN>.
Each child node contains a text node:
- "Yashu Talk About Eating"
- "Liang Shiqiu"
- "Jiangsu Literature and Art Publishing House"
- "2012"
- "48.00"
- "9787100011105"
Text is always stored in the text node
A common mistake in DOM processing is to think that the element node contains text.
However, the text of the element node is stored in the text node.
In this example:<year>2013</year>
, the element node <year> contains a text node with the value "2012".
"2012" Is not The value of the <year> element!
XML DOM node tree
The XML DOM views the XML document as a tree structure. This tree structure is calledNode tree.
All nodes can be accessed through this tree. You can modify or delete their content, or create new elements.
This node tree shows a collection of nodes and their relationships. This tree starts from the root node and then branches out to text nodes at the lowest level of the tree:

The figure above represents an XML file books.xml.
Parent, child, and sibling nodes
Nodes in the node tree have a hierarchical relationship with each other.
The terms 'parent', 'child', and 'sibling' are used to describe this relationship. A parent node has child nodes. Child nodes that are at the same level are called sibling nodes or sibling nodes (brothers or sisters).
- In the node tree, the top node is called the root
- Except for the root node, each node has only one parent node
- Nodes can have any number of child nodes
- Leaf nodes are nodes without child nodes
- Sibling nodes are nodes that have the same parent node
The following figure shows a part of the node tree and the relationships between nodes:

Because XML data is constructed in a tree form, it can be traversed without knowing the exact structure of the tree or the data types contained within it.
You will learn more about traversing the node tree in the later chapters of this tutorial.
Comments:Parent node: Parent Node, child node: Children Node, sibling node: Sibling Node.
First child node - last child node
Please check the following XML snippet:
<bookstore> <book category="美食"> <title lang="zh">雅舍谈吃</title> <author>梁实秋</author> <press>江苏文艺出版社</press> <year>2013</year> <price>35</price> <ISBN>9787100011105</ISBN> </book> </bookstore>
In the above XML, the <title> element is the first child of the <book> element, and the <ISBN> element is the last child of the <book> element.
In the above XML, the <title> element is the first child of the <book> element, and the <ISBN> element is the last child of the <book> element.
- Previous Page DOM Introduction
- Next Page DOM Access Node