Accessing Nodes in XML DOM

Through DOM, you can access each node in the XML document.

Accessing Nodes

There are three ways to access a node:

  • By using the getElementsByTagName() method
  • By traversing the node tree
  • By utilizing the relationship between nodes to navigate in the node tree

getElementsByTagName() method

getElementsByTagName() Returns all elements with the specified tag name.

Syntax

node.getElementsByTagName("tagname");

Example

The following example returns all <title> elements under the x element:

x.getElementsByTagName("title");

Note that the above example only returns the <title> elements under the x node. To return all <title> elements in the XML document, please use:

xmlDoc.getElementsByTagName("title");

Here,xmlDoc is the document itself (the document node).

List of DOM nodes

getElementsByTagName() The method returns a node list (node list). The node list is an array of nodes.

x = xmlDoc.getElementsByTagName("title");

You can access the <title> element in x by index number. If you need to access the third <title>, you can write it like this:

y = x[2];

Note:The index starts from 0.

Try it yourself

DOM node list length

length Attribute to define the length of the node list (number of nodes).

You can use length Attribute to loop through the node list:

Example

var x = xmlDoc.getElementsByTagName("title");
for (i = 0; i < x.length; i++) {
  // You can write code to process each node here
  }

Try it yourself

Node type

The nodes of the XML document documentElement The attribute is the root node.

The attribute is the node's nodeName The attribute is the node's name.

The attribute is the node's nodeType Attributes are the type of the node.

You will learn more about node attributes in the next chapter of this tutorial.

Try it yourself

Traversal of nodes

The following code loops through the child nodes of the root node (which are also element nodes):

Example

txt = "";
x = xmlDoc.documentElement.childNodes;
for (i = 0; i < x.length; i++) {
  // Process only element nodes (type 1)
  if (x[i].nodeType == 1) {
    txt += x[i].nodeName + "<br>";
  }
}

Try it yourself

Example explanation:

  1. Assuming you have already loaded "books.xml"Load into xmlDoc
  2. Get the child nodes of the root element (xmlDoc)
  3. Check the node type of each child node. If the node type is "1", it is an element node
  4. If it is an element node, then output the node name

Navigation node relationships

The following code navigates through the node tree by utilizing the relationships between nodes:

Example

x = xmlDoc.getElementsByTagName("book")[0];
xlen = x.childNodes.length;
y = x.firstChild;
txt = "";
for (i = 0; i < xlen; i++) {
  // Only handle element nodes (type 1)
  if (y.nodeType == 1) {
    txt += y.nodeName + "<br>";
  }
  y = y.nextSibling;
}

Try it yourself

Example explanation:

  1. Assuming you have already loaded "books.xml"Load into xmlDoc
  2. Get the child nodes of the first 'book' element
  3. Set the "y" variable to the first child node of the first book element
  4. For each child node (from the first child node "y" start):
  5. Check the node type of each child node. If the node type is "1" it is an element node
  6. If it is an element node, then output the name of the node
  7. Set "y" Variable is set to the next sibling node, and the loop is run again