Easy Tutorial
❮ Met Element Hasattributens Dom Prop Node Nodetype ❯

XML DOM - Accessing Nodes


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


Try It Yourself - Example

The following example uses the XML file books.xml. The loadXMLDoc() function, located in an external JavaScript, is used to load the XML file.

Accessing Nodes by Index from Node List

Traversing Nodes Using the Length Property

Checking the Node Type of an Element

Traversing Element Nodes

Navigating Element Nodes Using Node Relationships


Accessing Nodes

You can access nodes in three ways:

  1. By using the getElementsByTagName() method.

  2. By looping through (traversing) the node tree.

  3. By navigating through the node tree using node relationships.


getElementsByTagName() Method

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

Syntax

Example

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

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

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


DOM Node List

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

The following code uses loadXMLDoc() to load "books.xml" into xmlDoc and stores a list of <title> nodes in the variable x:

You can access the <title> elements in x by their index number. To access the third <title>, you can write:

Note: The index starts at 0.

You will learn more about node lists in later chapters of this tutorial.


DOM Node List Length

The length property defines the length of the node list (the number of nodes).

You can loop through the node list using the length property:

Example

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("title");

for (i=0;i&lt;x.length;i++)
{
  document.write(x[i].childNodes[0].nodeValue);
  document.write("");
}

Example explanation:


Node Types

The documentElement property of an 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 node properties in the next chapter of this tutorial.

Try It Yourself


Traversing Nodes

The following code traverses through the child nodes of the root node, which are also element nodes:

Example

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.documentElement.childNodes;

for (i=0;i&lt;x.length;i++)
{
  if (x[i].nodeType==1)
  {
    // Process once
    document.write(x[i].nodeName);
    document.write("");
  }
}

Example explanation:


Navigating Node Relationships

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

Example

xmlDoc=loadXMLDoc("books.xml");

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

for (i=0;i&lt;x.length;i++)
{
  if (y.nodeType==1)
  {
    // Output node name
    document.write(y.nodeName + "");
  }
  y=y.nextSibling;
}
❮ Met Element Hasattributens Dom Prop Node Nodetype ❯