OBJECT:  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. To demonstrate this Object we shall use the following simple XML file 'library.xml'.

      <title>Eyeless in Gaza</title>
      <author>Aldous Huxley</author>
      <title>John Barleycorn</title>
      <author>Jack London</author>

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.

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

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).nodeValue + "<br>");

Eyeless in Gaza
John Barleycorn

denotes a Microsoft extension to the W3C DOM.


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

Syntax: NodeList.length


item Method
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.

Syntax: NodeList.item(index)

nextNode Method
This method returns the next node in the collection.

Syntax: NodeList.nextNode( )

reset Method
This method resets the iterator for the collection.

Syntax: NodeList.reset( )

Copyright 1999-2001 by Infinite Software Solutions, Inc. All rights reserved.
Trademark Information