Property Accessors


E4X syntax reuses and extends the property accessor syntax already familiar to ActionScript developers. For example, ActionScript developers are already familiar with how to use dot-notation to access a property such as the x or y properties of a display object.

sprite.x = 100; sprite.y = 100;


Because of this familiarity, the learning curve for XML in ActionScript 3.0 is lowered.

For the following discussion we'll use this XML object.

var catalog:XML = <catalog>    <product >       <name>Product One</name>       <price>50</price>    </product>    <product >       <name>Product Two</name>       <price>35</price>    </product> </catalog>;


First, we'll access the XMLList that is a child of the root node. Because the child nodes of the root are called product, you can use dot notation to access a property of that same name which will reference the XMLList:

var products:XMLList = catalog.product;


XMLList objects allow you to use array-access notation to access the elements of the list. For example, the following code retrieves a reference to the first product XML node.

var firstProduct:XML = catalog.product[0];


Although up to this point the syntax has been familiar, we'll now introduce one new syntax that uses an @ symbol to reference attributes. By using the @ symbol, you can target attributes of an XML object as shown in the following example. This code retrieves the id attribute value of the first product node.

var id:Number = Number(catalog.product[0].@id);


If you use the @ symbol for an XMLList, it will return an XMLList of all the attributes of that name for all the XMLList elements. Here's an example that retrieves all the id attributes for all the product nodes:

var allProductIds:XMLList = catalog.product.@id;


You can use an asterisk as a wildcard to get all the items at a given level. For example, the following code retrieves all the product nodes without having to know the name of the nodes:

var firstLevel:XMLList = catalog.*;


You can also use wildcards with attributes. This example retrieves all the attributes of all the product nodes:

var firstLevel:XMLList = catalog.product.@*;





Advanced ActionScript 3 with Design Patterns
Advanced ActionScript 3 with Design Patterns
ISBN: 0321426568
EAN: 2147483647
Year: 2004
Pages: 132

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