O'Reilly logo

Learning ActionScript 3.0 by Zevan Rosser, Rich Shupe

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Reading XML

E4X makes reading XML easier than ever before. You can now use syntax consistent with use of other ActionScript objects, including not only properties and methods, but also individual nodes and attributes within an XML instance. For the examples in this section, we will be referring to the following XML object, book, and can be seen in readXML.fla.

var book:XML;

function createBasicStructure():void {
    book =  <book>
                <publisher name="O'Reilly"/>
                <title>Learning ActionScript 3.0</title>
                <subject>ActionScript</subject>
                <authors>
                    <author>
                        <firstname>Rich</firstname>
                        <lastname>Shupe</lastname>
                    </author>
                    <author>
                        <firstname>Zevan</firstname>
                        <lastname>Rosser</lastname>
                    </author>
                </authors>
            </book>;
    trace(book);
}
createBasicStructure();

A familial relationship is used to describe nodes. Nested element nodes are children of their parent element nodes, and text nodes and comments are children of their parent element nodes. Nodes at the same level are known as siblings.

Element Nodes

Retrieving a single element node from an XML object is as easy as drilling down from biggest to smallest, just like you would access a nested movie clip from the main timeline. For example, to access the title tag, you need only use the following script. Metaphorically, the XML instance is the main timeline and title is the movie clip. The Output panel results are displayed in comments.

trace(book.title);
//Learning ActionScript 3.0

Note

If you're wondering why the syntax isn't trace(book.book); it's because ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required