XML DOM Node Lists
Node lists are returned by the getElementsByTagName() method and the childNodes property.
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.
From the first
This example uses the getElementsByTagName() method to get the first element in "books.xml"
Use the length property to traverse nodes
This example uses the node list and the length property to iterate through all the nodes in "books.xml"
Get the attributes of an element
This example uses the attribute list from the first element in "books.xml"
DOM Node List (Node List)
when using properties or methods such as childNodes or getElementsByTagName(), a node list object is returned.
A node list object represents a list of nodes, in the same order as in the XML.
Nodes in a node list are accessed using index numbers starting from 0.
The following image shows "books.xml" in

The following code snippet by using loadXMLDoc() Put the "books.xml" loaded into xmlDoc, and returns a node list of title elements in "books.xml":
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title");
After the above statement is executed, x is a node list object.
The following code snippet gets the first from the node list (x)
Example
txt
=
x
[
0
]
.
childNodes
[
0
]
.
nodeValue
;
Try it »
After the above statement is executed, txt = "Everyday Italian".
Node List Length (Node List Length)
A node list object keeps itself updated. If elements are removed or added, the list updates automatically.
The length property of a node list is the number of nodes in the list.
The following code snippet by using loadXMLDoc() Put the "books.xml" loaded into xmlDoc, and returns in "books.xml"
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('title').length;
After the above statement is executed, x = 4.
The length of a node list can be used to iterate over all the elements in the list.
The following code snippet uses the length property to traverse
Example
xmlDoc
=
loadXMLDoc
(
"
books.xml
"
)
;
//
The x variable gets the specified node list
x
=
xmlDoc
.
getElementsByTagName
(
'
title
'
)
;
for
(
i
=
0
;
i
<
x
.
length
;
i
++
)
{
document
.
write
(
x
[
i
]
.
childNodes
[
0
]
.
nodeValue
)
;
document
.
write
(
"
"
)
;
}
Output:
Everyday Italian
Harry Potter
XQuery Kick Start
Learning XML
Try it »
Example explained:
- Use loadXMLDoc() Put the "books.xml" loaded into xmlDoc
DOM Attribute Lists (Named Node Map)
The attributes property of an element node returns a list of attribute nodes.
This is called a Named Node Map; apart from some differences in methods and properties, it is similar to a node list.
The attribute list keeps itself updated. If an attribute is deleted or added, the list is automatically updated.
The following code snippet uses loadXMLDoc() to load "books.xml" load into xmlDoc, and return the first element in "books.xml"
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book')[0].attributes;
After the code above is executed, x.length equals the number of attributes, and x.getNamedItem() can be used to return an attribute node.
The following code snippet shows the value of a book's "category" attribute, as well as the number of its attributes:
Example
xmlDoc
=
loadXMLDoc
(
"
books.xml
"
)
;
x
=
xmlDoc
.
getElementsByTagName
(
"
book
"
)
[
0
]
.
attributes
;
document
.
write
(
x
.
getNamedItem
(
"
category
"
)
.
nodeValue
)
;
document
.
write
(
"
"
x
.
length
)
;
Output:
cooking
1
Try it »
Example explanation:
- Use loadXMLDoc() to load "books.xml" load into xmlDoc
- Set the x variable to hold the first
list of all attributes of the element - Output the value from the "category" attribute
- Output the length of the attribute list