XML.childNodes Property

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 18.  ActionScript Language Reference
XML.childNodes Property Flash 5

an array of references to a node's children read-only
theNode.childNodes[n]

Description

The childNodes property is an array whose elements contain references to the immediate descendants of theNode, where theNode can be an XML or XMLnode instance. The childNodes property is used to access nodes in an XML hierarchy. For example, if we create an object hierarchy as follows:

myDoc = new XML('<STUDENT><NAME>Tim</NAME><MAJOR>BIOLOGY</MAJOR></STUDENT>');

we can then access the STUDENT node using:

myDoc.childNodes[0];

We can access the NAME and MAJOR nodes (which are descendants of STUDENT) using:

myDoc.childNodes[0].childNodes[0];     // NAME myDoc.childNodes[0].childNodes[1];     // MAJOR

If the hierarchy below theNode changes, childNodes is automatically updated to reflect the new structure. For example, if we delete the MAJOR node, myDoc.childNodes[0].childNodes[1] will return undefined.

We often refer to nodes to manipulate information or rearrange a document's structure. For example, we can change a student's name or add a new student using:

// Check the name of the student trace("The student's name is: "        + myDoc.childNodes[0].childNodes[0].childNodes[0].nodeValue);     // Change the name of the student myDoc.childNodes[0].childNodes[0].childNodes[0].nodeValue = "James";     // Copy the STUDENT node newStudent = myDoc.childNodes[0].cloneNode(true);     // Add a new STUDENT node to the document myDoc.appendChild(newStudent);

Note that, as a convenience, we can also use the firstChild property to refer to childNodes[0]. The following references are identical:

myDoc.childNodes[0]; myDoc.firstChild;

To iterate through all the children of a node, we can use a for statement, as follows:

for (var i = 0; i < theNode.childNodes.length; i++) {   trace("child " + i + " is " + theNode.childNodes[i].nodeName); }

However, our example traverses only the first level of theNode's hierarchy. The examples under the XML.nextSibling entry show how to access all the nodes below theNode. If theNode has no children, theNode.childNodes.length is 0.

Usage

Remember that empty text nodes, representing the whitespace used to format XML source code, also show up in a childNode list. For example, in the following XML source, empty text nodes are created by the whitespace after the BOOK start tag and the TITLE, AUTHOR, and PUBLISHER end tags:

<BOOK>   <TITLE>ActionScript for Flash MX: The Definitive Guide</TITLE>   <AUTHOR SALUTATION="Mr.">Colin Moock</AUTHOR>   <PUBLISHER>O'Reilly</PUBLISHER> </BOOK>

Hence, the first child node of BOOK is an empty text node; the second child is TITLE. To remove whitespace, use XML.ignoreWhite.

Bugs

In Flash Player 5, an XML document with an XML declaration (<?xml version="1.0"?>) has a childNodes[0] of undefined. The first child of the document is stored erroneously in childNodes[1]. This bug was fixed in Flash Player 6.

See Also

XML.firstChild, XML.hasChildNodes( ), XML.lastChild, XML.nextSibling, XML.previousSibling



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net