XML DOM - Accessing Nodes

With the DOM, you can access every node in an XML document.

Accessing Nodes

You can access a node in three ways:
1. By using the getElementsByTagName() method
2. By looping through (traversing) the nodes tree.
3. By navigating the node tree, using the node relationships.

The getElementsByTagName() Method

getElementsByTagName() returns all elements with a 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 example above only returns <title> elements under the x node. To return all <title> elements in the XML document use:
xmlDoc.getElementsByTagName("title");
where xmlDoc is the document itself (document node).

DOM Node List

The getElementsByTagName() method returns a node list. A node list is an array of nodes.
x = xmlDoc.getElementsByTagName("title");
The <title> elements in x can be accessed by index number. To access the third <title> you can write::
y = x[2];
Note: The index starts at 0.
You will learn more about node lists in a later chapter of this tutorial.

DOM Node List Length

The length property defines the length of a node list (the number of nodes).
You can loop through a node list by using the length property:

Example

var x = xmlDoc.getElementsByTagName("title");

for (i = 0; i <x.length; i++) {
  // do something for each node  }

Node Types

The documentElement property of the XML document is the root node.
The nodeName property of a node is the name of the node.
The nodeType property of a node is the type of the node.
You will learn more about the node properties in the next chapter of this tutorial.

Traversing Nodes

The following code loops through the child nodes, that are also element nodes, of the root node:

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>";
  }
}
Example explained:
  1. Suppose you have loaded "books.xml" into xmlDoc
  2. Get the child nodes of the root element (xmlDoc)
  3. For each child node, check the node type. If the node type is "1" it is an element node
  4. Output the name of the node if it is an element node

Navigating Node Relationships

The following code navigates the node tree using the node relationships:

Example

x = xmlDoc.getElementsByTagName("book")[0];
xlen = x.childNodes.length;
y = x.firstChild;

txt = "";
for (i = 0; i <xlen; i++) {
  // Process only element nodes (type 1)  if (y.nodeType == 1) {
    txt += y.nodeName + "<br>";
  }
  y = y.nextSibling;
}
Example explained:
  1. Suppose you have loaded "books.xml"into xmlDoc
  2. Get the child nodes of the first book element
  3. Set the "y" variable to be the first child node of the first book element
  4. For each child node (starting with the first child node "y"):
  5. Check the node type. If the node type is "1" it is an element node
  6. Output the name of the node if it is an element node
  7. Set the "y" variable to be the next sibling node, and run through the loop again