User Guide
330 Working with XML
{
myXML.total.children()[0] = Number(myXML.total.children()[0])
+ Number(item.price.children()[0]);
}
trace(myXML.total); // 4.35;
If this code did not use the Number() function, the code would interpret the + operator as the
string concatenation operator, and the
trace() method in the last line would output the
following:
01.003.95
Reading external XML documents
You can use the URLLoader class to load XML data from a URL. To use the following code in
your applications, replace the
XML_URL value in the example with a valid URL:
var myXML:XML = new XML();
var XML_URL:String = "http://www.example.com/Sample3.xml";
var myXMLURL:URLRequest = new URLRequest(XML_URL);
var myLoader:URLLoader = new URLLoader(myXMLURL);
myLoader.addEventListener("complete", xmlLoaded);
function xmlLoaded(evtObj:Event):void
{
myXML = XML(myLoader.data);
trace("Data loaded.");
}
You can also use the XMLSocket class to set up an asynchronous XML socket connection
with a server. For more information, see the ActionScript 3.0 Language Reference.
Example: Loading RSS data from the
Internet
The RSSViewer sample application shows a number of features of working with XML in
ActionScript, including the following:
■ Using XML methods to traverse XML data in the form of an RSS feed.
■ Using XML methods to assemble XML data in the form of HTML to use in a text field.