XML DOM - Accessing Nodes
Through the DOM, you can access every node in an XML document.
Try it - Example
The following example uses the XML file books.xml.
function loadXMLDoc(), located in an external JavaScript, used to load the XML file.
Access nodes using their index in the node list
This example uses the getElementsByTagname() method to get the third element in "books.xml"
Use the length property to traverse the nodes
This example uses the length property to iterate through all the elements in "books.xml"
View the node type of an element
This example uses the nodeType property to get the node type of the root element in "books.xml".
Traverse element nodes
This example uses the nodeType property to handle the element nodes in "books.xml".
Use the relationships between nodes to traverse element nodes
This example uses the nodeType property and the nextSibling property to handle the element nodes in "books.xml".
Access nodes
You can access nodes in three ways:
-
By using the getElementsByTagName() method.
-
By looping (traversing) through the node tree.
-
Navigate in the node tree by using the relationships between nodes.
getElementsByTagName() Method
getElementsByTagName() returns all elements with the specified tag name.
Syntax
node
.getElementsByTagName(
"tagname"
);
Example
The following example returns all the elements under the x element
x.getElementsByTagName("title");
Please note that the example above only returns the ones under node x
xmlDoc.getElementsByTagName("title");
Here, xmlDoc is the document itself (the document node).
DOM Node List (Node List)
The getElementsByTagName() method returns a node list. A node list is an array of nodes.
The following code uses loadXMLDoc() Put the "books.xml" loaded into xmlDoc, then stored in variable x
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title");
can be accessed by index number in x
y=x[2];
**Note:** The index starts from 0.
In later chapters of this tutorial, you will learn more about node lists (Node List).
DOM Node List Length (Node List Length)
The length property defines the length of a node list (that is, the number of nodes).
You can iterate through a node list by 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
(
"
"
)
;
}
Try it »
Example explained:
- Use loadXMLDoc() Put the "books.xml" loaded into xmlDoc
- Get all
element node - Output each
the value of the element's text node
Node Types (Node Types)
of an XML document documentElement the attribute is the root node.
The node's nodeName the attribute is the name of the node.
The node's nodeType the attribute is the type of the node.
You will learn more about node properties in the next chapter of this tutorial.
Traverse nodes
The following code traverses the child nodes of the root node that 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
)
{
//
Execute once
document
.
write
(
x
[
i
]
.
nodeName
)
;
document
.
write
(
"
"
)
;
}
}
Try it »
Example explanation:
- Use loadXMLDoc() to load "books.xml" load 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's name
Navigate node relationships
The following code uses node relationships to navigate the node tree:
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 the node name
document
.
write
(
y
.
nodeName
"
"
)
;
}
y
=
y
.
nextSibling
;
}
Try it »
- Use loadXMLDoc() to load "books.xml" load into xmlDoc
- Get the child nodes of the first book element
- Set the "y" variable to the first child node of the first book element
- For each child node (starting from "y" for the first child node), check the node type; if the node type is "1", it is an element node
- If it is an element node, output the node's name
- Set the "y" variable to the next sibling node, and run the loop again