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

Properties
length Retrieves the number of elements in the collection.

Methods
item Retrieves an object from the collection by its index.

Remarks
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.
Examples

Suppose you have an XML document structured like the following:

<?xml version="1.0"?>
<?access-control allow="*"?>
<subjects>
   <subject id="s1">
      <name>Overview</name>
   </subject>
   <subject id="s2">
      <name>Design</name>
   </subject>
   <subject id="s3">
      <name>Development</name>
   </subject>
</subjects>


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"
  xmlns="http://www.w3.org/2001/vxml">

  <script>
  <![CDATA[
  // 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') {
        subjects.push(child);
      }
    }
    return subjects;
  }
  ]]></script>

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

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


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