* * *
Node lists are returned by the getElementsByTagName() method and the childNodes property.
* * *
Try it Yourself - Example
The following example uses the XML file books.xml.
The function loadXMLDoc(), located in an external JavaScript, is used to load the XML file.
Get text from the first
This example uses the getElementsByTagName() method to get text from the first
Traverse nodes using the length property
This example uses a node list and the length property to traverse all
This example uses an attribute list to get attributes from the first element in "books.xml".
* * *
DOM Node List
When using properties or methods like childNodes or getElementsByTagName(), a node list object is returned.
A node list object represents a list of nodes, in the order they appear in the XML.
The nodes in a node list can be accessed by an index number, starting at 0.
The image below shows a node list of the
The code snippet below loads "books.xml" into xmlDoc using loadXMLDoc(), and returns a node list of title elements in "books.xml":
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title");
After the statements above are executed, x is a node list object.
The code snippet below returns the text from the first
Example
txt=x.childNodes.nodeValue;
After the statement above is executed, txt = "Everyday Italian".
* * *
Node List Length
A node list object maintains itself automatically. If elements are deleted or added, the list updates itself.
The length property of a node list is the number of nodes in the list.
The code snippet below loads "books.xml" into xmlDoc using loadXMLDoc(), and returns the number of
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('title').length;
After the statements above are executed, x = 4.
The length of a node list can be used to traverse all elements in the list.
The code snippet below uses the length property to traverse the list of
Example
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName('title'); for(i=0;i<x.length;i++){document.write(x.childNodes.nodeValue); document.write("
"); }
Output:
Everyday Italian
Harry Potter
XQuery Kick Start
Learning XML
Explanation of the example:
- Load "books.xml" into xmlDoc using loadXMLDoc()
- Set the x variable to hold a list of all title elements
- Output the value of the text node from all
elements
* * *
DOM Named Node Map (Attribute List)
The attributes property of an element node returns a list of attribute nodes.
This is called a named node map (Named Node Map), which is similar to a node list, except for some differences in methods and properties.
An attribute list maintains itself automatically. If attributes are deleted or added, the list updates itself.
The code snippet below loads "books.xml" into xmlDoc using loadXMLDoc(), and returns a list of attribute nodes for the first element in "books.xml":
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName('book').attributes;
After the code above is executed, x.length equals the number of attributes, and x.getNamedItem() can return an attribute node.
The code snippet below displays the value of the "category" attribute of a book, as well as the number of attributes:
Example
xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("book").attributes; document.write(x.getNamedItem("category").nodeValue); document.write("
" + x.length);
Output:
cooking
1
Explanation of the example:
- Load "books.xml" into xmlDoc using loadXMLDoc()
- Set the x variable to hold a list of all attributes of the first element
- Output the value of the "category" attribute
- Output the length of the attribute list
YouTip