ADOBE FLEX 3
Developer Guide
211
As the following code shows, E4X includes some intuitive operators, such as the dot (.) and attribute identifier (@)
operators, for accessing properties and attributes in the XML:
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() method to assign a new child node to the XML, as the following 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 for loop to iterate through nodes of the XML, as follows:
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));
XML objects
An XML object may represent an XML element, attribute, comment, processing instruction, or text element.
An XML object is classified as having either simple content or complex content. An XML object that has child nodes
is classified as having complex content. An XML object is said to have simple content if it is any one of the following:
an attribute, a comment, a processing instruction, or a text node.
For example, the following XML object contains complex content, including a comment and a processing
instruction:
XML.ignoreComments = false;
XML.ignoreProcessingInstructions = false;
var x1:XML =
<order>
<!--This is a comment. -->
<?PROC_INSTR sample ?>
<item id='1'>
<menuName>burger</menuName>
ADOBE FLEX 3
Developer Guide
212
<price>3.95</price>
</item>
<item id='2'>
<menuName>fries</menuName>
<price>1.45</price>
</item>
</order>
As the following example shows, you can now use the comments() and processingInstructions() methods to
create new XML objects, a comment and a processing instruction:
var x2:XML = x1.comments()[0];
var x3:XML = x1.processingInstructions()[0];
XML properties
The XML class has five static properties:
The ignoreComments and ignoreProcessingInstructions properties determine whether comments or
processing instructions are ignored when the XML object is parsed.
The ignoreWhitespace property determines whether white space characters are ignored in element tags and
embedded expressions that are separated only by white space characters.
The prettyIndent and prettyPrinting properties are used to format the text that is returned by the
toString() and toXMLString() methods of the XML class.
For details on these properties, see the ActionScript 3.0 Language and Components Reference.
XML methods
The following methods allow you to work with the hierarchical structure of XML objects:
appendChild()
child()
childIndex()
children()
descendants()
elements()
insertChildAfter()
insertChildBefore()
parent()
prependChild()
The following methods allow you to work with XML object attributes:
attribute()
attributes()
The following methods allow you to you work with XML object properties:
hasOwnProperty()
propertyIsEnumerable()
ADOBE FLEX 3
Developer Guide
213
replace()
setChildren()
The following methods are for working with qualified names and namespaces:
addNamespace()
inScopeNamespaces()
localName()
name()
namespace()
namespaceDeclarations()
removeNamespace()
setLocalName()
setName()
setNamespace()
The following methods are for working with and determining certain types of XML content:
comments()
hasComplexContent()
hasSimpleContent()
nodeKind()
processingInstructions()
text()
The following methods are for conversion to strings and for formatting XML objects:
defaultSettings()
setSettings()
settings()
normalize()
toString()
toXMLString()
There are a few additional methods:
contains()
copy()
valueOf()
length()
For details on these methods, see the ActionScript 3.0 Language and Components Reference.

Get ADOBE® FLEX® 3: PROGRAMMING ACTIONSCRIPT™ 3.0 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.