Skip to main content

XML DOM Nodes

According to the XML DOM, everything in an XML document is a node.

Therefore:

  • The entire document is a document node
  • Every XML element is an element node
  • The text in the XML elements are text nodes
  • Every attribute is an attribute node
  • Comments are comment nodes

Node Types

XML DOM Node Types

The most common types of nodes in XML are:

  • Document Node: complete XML document structure is a document node.
  • Element Node: every XML element is an element node. This is also the only type of node that can have attributes.
  • Attribute Node: each attribute is considered an attribute node. It contains information about an element node, but is not actually considered to be children of the element.
  • Text Node: the document texts are considered as text node. It can consist of more information or just white space.

But there are other type of nodes (less common types):

  • CData Node: this node contains information that should not be analyzed by the parser. Instead, it should just be passed on as plain text.
  • Comment Node this node includes information about the data, and is usually ignored by the application.
  • Processing Instructions Node: This node contains information specifically aimed at the application.
  • Document Fragments Node
  • Entities Node
  • Entity reference nodes
  • Notations Node

XML DOM Node Tree

The XML DOM represents an XML document as a tree-structure. The tree structure is called a node-tree.

All nodes can be accessed through the tree. Their contents can be modified or deleted, and new elements can be created.

The node tree shows the set of nodes, and the connections between them. The tree starts at the root node and branches out to the text nodes at the lowest level of the tree

Node Parents, Children, and Siblings in XML DOM

Nodes in the node tree have a hierarchical relationship to each other.

The terms parent, child, and sibling are used to describe relationships: parents nodes have children and children at the same level are called siblings.

More in detail:

  • In a tree of nodes, the top node is called the root
  • Each node, except the root, has exactly one parent node
  • A node can have any number of children
  • A leaf is a node with no children
  • Siblings are nodes with the same parent
note

Since XML data is structured as a tree, it can be traversed without knowing the exact structure of the tree and without knowing the type of data contained within it.

Example: First Child and Last Child

Consider this XML fragment:

<bookstore>
<book category="cooking">
<title lang="en">A Great Book</title>
<author>Tom Nolan</author>
<year>2022</year>
<price>19.90</price>
</book>
</bookstore>

You can observe that the <title> element is the first child of the <book> element, and the <price> element is the last child of the <book> element.

Furthermore, the <book> element is the parent node of the <title>, <author>, <year>, and <price> elements.