Adobe FLEX 2-PROGRAMMING ACTIONSCRIPT 3.0 Manual page 314

Programming actionscript 3.0
Table of Contents

Advertisement

Often, your application will load XML data from an external source, such as a web service or
a RSS feed. However, for clarity, the examples in this chapter assign XML data as literals.
As the following code shows, E4X includes some intuitive operators, such as the dot (
attribute identifier (
@
trace(myXML.item[0].menuName); // Output: burger
trace(myXML.item.(@id==2).menuName); // Output: fries
trace(myXML.item.(menuName=="burger").price); // Output: 3.95
Use the
appendChild()
snippet shows:
var newItem:XML =
<item id="3">
<menuName>medium cola</menuName>
<price>1.25</price>
</item>
myXML.appendChild(newItem);
Use the
and
operators not only to read data, but also to assign data, as in the following:
@
.
myXML.item[0].menuName="regular burger";
myXML.item[1].menuName="small fries";
myXML.item[2].menuName="medium cola";
myXML.item.(menuName=="regular burger").@quantity = "2";
myXML.item.(menuName=="small fries").@quantity = "2";
myXML.item.(menuName=="medium cola").@quantity = "2";
Use a
loop to iterate through nodes of the XML, as follows:
for
var total:Number = 0;
for each (var property:XML in myXML.item)
{
var q:int = Number(property.@quantity);
var p:Number = Number(property.price);
var itemTotal:Number = q * p;
total += itemTotal;
trace(q + " " + property.menuName + " $" + itemTotal.toFixed(2))
}
trace("Total: $", total.toFixed(2));
314
Working with XML
) operators, for accessing properties and attributes in the XML:
method to assign a new child node to the XML, as the following
) and
.

Advertisement

Table of Contents
loading

This manual is also suitable for:

Flex

Table of Contents