NodeList Object
Last Updated: 08-30-2005
Represents an ordered collection of node objects in the Document Object Model (DOM).

length Retrieves the number of elements in the collection.

item Retrieves an object from the collection by its index.

A NodeList object is returned by the childNodes property of a Node, and by the getElementsByTagName and getElementsByTagNameNS methods.
You determine the number of elements in a NodeList by checking its length property.
The items in the NodeList are accessible via an integral index, starting from 0, using the item method.

Suppose you have an XML document structured like the following:

<?xml version="1.0"?>
<?access-control allow="*"?>
   <subject id="s1">
   <subject id="s2">
   <subject id="s3">

The following program can be used to extract and report the subjects. It executes a data element to fetch an XML document. It then calls the GetSubjects function to process the document and extract the subjects. GetSubjects uses the childNodes property to get the list of children of the root document element. It then iterates through the list, using the item method to get each child node in turn. For each child, if its nodeType is Node.ELEMENT_NODE and its nodeName is "subject", then the program adds that node to the subjects array. After GetSubjects has added all the element child nodes to the array, it returns the array and the program goes through the array to report the different subjects.

<vxml version="2.1"

  // given the root document element of an XML DOM, 
  // return the 'subject' elements
  function GetSubjects(rootNode)
    var subjects = [];
    var oNodeList = rootNode.childNodes;
    var len = oNodeList.length;
    for (var i = 0; i < len; i++) {
      var child = oNodeList.item(i);
      if (child.nodeType == Node.ELEMENT_NODE && 
          child.nodeName == 'subject') {
    return subjects;

  <data name="dom" src="subjects.xml"/>

      <var name="subjects" expr="GetSubjects(dom.documentElement)"/>
      <prompt>The subjects are</prompt>
      <foreach item="subject" array="subjects">
        <value expr="subject"/>
        <break time="300ms"/>

[24]7 Inc.| Terms of Service| Privacy Policy| General Disclaimers