childNodes Property
Last Updated: 08-27-2005
Retrieves a NodeList collection of node objects.

Syntax
var oNodes = Node.childNodes;

Possible Values
oNodes A NodeList collection of node objects.

Remarks
If a node doesn't contain any children, the length property of the childNodes property returns the value 0. The hasChildNodes method returns false.
Before calling the item method on the object returned by the childNodes property of a node object, check the value of the length property to ensure that it is greater than zero, or call the node's hasChildNodes method and check for the value true.
Examples

The example below utilizes the following sample data describing a stock quote.

<?xml version="1.0"?>
<?access-control allow="*"?>
<quote>
   <name>Ford Motor Company</name>
   <ticker>F</ticker>
   <open>28.35</open>
   <last>28.72</last>
   <change>.54</change>
   <day_high>29.03</day_high>
   <day_low>28.03</day_low>
</quote>

The following example reads back information about a stockquote to the user. The Node2Array function uses the childNodes property of the root document element to retrieve a NodeList (oChildren) containing the children of that element. The length of the list is determined from the length property, The code iterates through the NodeList, calling the item method to obtain each child in turn and push its data onto an array.

<vxml version="2.0">
<data name="oQuote" src="quote.exml"/>

<catch event="error.badfetch">
   <log>Caught error.badfetch. Must have faulted loading data.</log>
</catch>

<script><![CDATA[

// hash table w/strings describing each node in the quote data
var hQuotePrompts = {
   "name" : "The company name is ",
   "ticker" : "The company ticker symbol is ",
   "open" : "The stock opened today at ",
   "last" : "The current price per share is ",
   "change" : "The change in value is ",
   "day_high" : "The day's high is ",
   "day_low" : "The day's low is "
};

// convert the DOM node to an array of objects. 
// Since we know the data, this code assumes the node contains 
// elements that contain children that are text nodes
function Node2Array(oNode)
{
  var a = []; // array to hold data

  // cache the collection of children
  var oChildren = oNode.childNodes;
  var iChildren = oChildren.length;

  // do we have any children to iterate through?
  if (0 == iChildren)
  {
     Log("no children encountered.");
     return a;
  }

  // iterate through the children of the root element;
  for (var i = 0; i < iChildren; i++)
  {
     // store the name of the node and the value of the text node contained within the node
     var oChild = oChildren.item(i);
     a.push({"name" : oChild.nodeName, "value" : oChild.firstChild.data});
  }

  return a;
}

function Log(s) { vxmllog(s); }
]]></script>


<form id="read_quote_data">
<block>
   <if cond="oQuote != null">
      <script><![CDATA[
         var aValues = Node2Array(oQuote.documentElement);
      ]]></script>
      <foreach item="oValue" array="aValues">
         <audio><value expr="hQuotePrompts[oValue.name]"/> <value expr="oValue.value"/></audio>
      </foreach>
   <else/>
      <log>Unable to load quote data. <value expr="typeof(oQuote)"/></log>
   </if>
</block>
</form>
</vxml>


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