XML DOM » Objects » NodeList

The NodeList object represents a live collection of Node objects. This means that any alterations to the number or properties of nodes is immediately reflected in the list. This list permits indexed access to individual nodes as well as iteration through the collection.

Examples

Code:
var xml_doc = new ActiveXObject("Microsoft.XMLDOM");
xml_doc.async = false;
xml_doc.load("library.xml");

var title_nodes = xml_doc.getElementsByTagName("title");
var n_titles = title_nodes.length
for (i = 0; i < n_titles; i++)
   document.write(title_nodes.item(i).text + "<br>");
Output:
Eyeless in Gaza
John Barleycorn
Explanation:

To demonstrate this Object we shall use the following simple XML file:

<library>
   <book>
      <category>fiction</category>
      <title>Eyeless in Gaza</title>
      <author>Aldous Huxley</author>
   </book>
   <book>
      <category>classics</category>
      <title>John Barleycorn</title>
      <author>Jack London</author>
   </book>
</library>

We shall now create a NodeList object of all the 'title' elements using the document's getElementsByTagName method. The number of nodes in the collection is determined using the length property, and their nodeValue properties are displayed by accessing each in turn through the item method.

Language(s): JavaScript

Properties

length

Syntax: object.length

This property returns the number of items in the NodeList collection.

Methods

item Method

Syntax: object.item.(index)

This method returns the item at the specified index of the Node collection. These are numbered from 0 to one less than the value of the length property. Using an invalid index returns null.

nextNode

Syntax: nodeList.nextNode()

This method returns the next node in the collection.

reset

Syntax: nodeList.reset

This method resets the iterator for the collection.