XML Filtering


Arguably, the most powerful and useful feature of E4X is its ability to filter data. This new feature replaces the need for XPath libraries and the loops previously used to search an XML document in earlier versions of ActionScript. Now you can do all this natively in ActionScript 3.0 using as little as one line of code.

To use filtering, you can enclose expressions in parentheses as part of an E4X expression. The following examples show a few uses of filtering. We'll use the same XML object for all our examples:

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


The following line retrieves the product element that has an id attribute equal to 1. This example returns just one XML object, but it would return an XMLList if more than one element matches the criteria.

var product1:XML = catalog.product.(@id == 1);


This next line grabs the name of the product element that has a price less than 50. It's important to understand that this would return an XMLList if more than one element met the criteria:

var cheapestProductName:String = catalog.product.(price < 50).name;


This example retrieves the product elements that have a price greater than 35 and less than 50:

var productRange:XMLList = catalog.product.(price >= 35 && price <= 50);





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