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
Navigating Element Nodes Using Node Relationships
Accessing Nodes
You can access nodes in three ways:
By using the getElementsByTagName() method.
By looping through (traversing) the node tree.
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<x.length;i++)
{
document.write(x[i].childNodes[0].nodeValue);
document.write("");
}
Example explanation:
Use loadXMLDoc() to load "books.xml" into xmlDoc
Get all <title> element nodes
Output the value of the text node for each <title> element
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.
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<x.length;i++)
{
if (x[i].nodeType==1)
{
// Process once
document.write(x[i].nodeName);
document.write("");
}
}
Example explanation:
Use loadXMLDoc() to load "books.xml" into xmlDoc
Get the child nodes of the root element
Check the node type of each child node. If the node type is "1", it is an element node
If it is an element node, output the node name
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<x.length;i++)
{
if (y.nodeType==1)
{
// Output node name
document.write(y.nodeName + "");
}
y=y.nextSibling;
}
Use loadXMLDoc() to load "books.xml" into xmlDoc
Get the child nodes of the first book element
Set the variable "y" to the first child node of the first book element
For each child node (starting with the first child node "y"), check the node type, if the node type is "1", it is an element node
If it is an element node, output the node name
Set the variable "y" to the next sibling node and repeat the loop