O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

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

Accessing XML Data

E4X offers two general sets of tools for accessing data in an XML hierarchy:

  • The XML and XMLList content-access methods (attribute( ), attributes( ), child( ), children( ), comments( ), descendants( ), elements( ), parent( ), processingInstructions( ), and text( ))

  • Variable-style access with the dot (.), descendant (..), and attribute (@) operators

Variable-style access is offered as a convenience to the programmer and always equates to one of the methods of either the XML or XMLList classes. However, the two approaches do not overlap completely; the following types of content must be accessed using the appropriate method of the XML or XMLList class:

  • An XML instance's parent (accessed via parent( ))

  • Comments (accessed via comments( ))

  • Processing instructions (accessed via processingInstructions( ))

  • Elements or attributes whose names include characters considered illegal in an ActionScript identifier (accessed via attribute( ), child( ), descendants( ), or elements( ))

Continuing with our <BOOK> example, let's take a look at some of the most common ways to access XML data.

Accessing the Root XML Node

In Example 18-2 we assigned the XML fragment from Example 18-1 to the variable novel. To access the root <BOOK> element of that fragment (item A in Figure 18-2) we refer to it as, simply, novel. For example, the following code passes the <BOOK> element (and, by extension, all its children) to the hypothetical addToOrder( ) method:

addToOrder(novel);

Notice that the <BOOK> element ...

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