XML.firstChild Property

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

a reference to the first descendant of a node read-only
theNode.firstChild

Description

The firstChild property is synonymous with childNodes[0]. It returns a reference to the first node object that descends from theNode, where theNode can be an XML or XMLnode instance. If theNode has no children, firstChild returns null.

In this XML source fragment, the firstChild of the MESSAGE node is the text node with the nodeValue "hey":

<!-- Fragment 1 --> <MESSAGE>hey</MESSAGE>

Here, the firstChild of the HOTEL node is the ROOM node:

<!-- Fragment 2 --> <HOTEL><ROOM><SIZE>Double</SIZE></ROOM></HOTEL>

When theNode is the top of the object hierarchy (i.e., when it is an instance of the XML class, which refers to an XML document object and not an instance of the XMLnode class), firstChild may not always be a reference to the first useful element in the document. If a document includes an XML declaration (<?xml version="1.0"?>) and perhaps a DOCTYPE tag, normally there are whitespace nodes before the actual root element of the XML hierarchy (to remove them, use XML.ignoreWhite). If an XML fragment has no XML declaration, no DOCTYPE, and no whitespace, we can start processing it with the document's firstChild node, as in:

// Create a new XML fragment myDoc = new XML('<MESSAGE><USER>gray</USER><CONTENT>hi</CONTENT></MESSAGE>');     // Store the XML fragment's first node in the variable msg msg = myDoc.firstChild;     // Assign the text contained by the USER tag // to a text field called userNameOutput userNameOutput = msg.firstChild.firstChild.nodeValue;

It's good form to use nodeValue to access the text contained by the USER tag. When we use a text-node object in a string context, the toString( ) method is automatically invoked on that node, and the text in the node is returned.

See Also

XML.childNodes, 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