With Actionscript 3.0 the XML Class seems to have deeply changed.
The XML class has been moved to the package flash.xml and its name has been changed to XMLDocument to avoid the conflict with the new XML Class, which has been enriched with the ECMAScript for XML.
I?ll begin with saying that the load() and onLoad() methods of AS 2.0 have been removed, therefore we now use the URLLoader and URLRequest classes to load an external XML file.
You add a listener to the instance of URLLoader and at the COMPLETE event, the XML parsing begins, hence you pass the data of the URLLoader?s instance to a new instance of the XML class, then you create a new instance of the XMLDocument class, which helps parsing the XML instance.
It?s easier done than said, so let?s see the juice:
This is the XML file ( very simple just to understand the example ):
HTML Code:
<?xml version="1.0" encoding="UTF-8"?>
<setting>
<parameters color="0xFFFFFF" name="Filippo" gen="male" age="34" picturePath="filippo.jpg">
</parameters>
</setting>
And this is the class I?ve built:
Code:
package
{
import flash.display.Loader;
import flash.display.MovieClip;
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.xml.*;
public class LoadingXML extends MovieClip
{
public function LoadingXML()
{
this.loadXML();
}
private function loadXML():void
{
var loader:URLLoader=new URLLoader();
loader.addEventListener(Event.COMPLETE,completeHandler);
var request:URLRequest=new URLRequest('setting.xml');
try
{
loader.load(request);
}
catch(error:Error)
{
trace('Unable to load requested document.');
}
}
private function completeHandler(event:Event):void
{
var loader:URLLoader=URLLoader(event.target);
var result:XML=new XML(loader.data);
var myXML:XMLDocument=new XMLDocument();
myXML.ignoreWhite=true;
myXML.parseXML(result.toXMLString());
var node:XMLNode=myXML.firstChild;
trace('Colore= '+node.firstChild.attributes['color']);
trace('Nome= '+node.firstChild.attributes['name']);
trace('Genere= '+node.firstChild.attributes['gen']);
trace('Età= '+node.firstChild.attributes['age']);
trace('Path immagine= '+node.firstChild.attributes['picturePath']);
}
}
}
And this is the resulting output:
Colore= 0xFFFFFF
Nome= Filippo
Genere= male
Età= 34
Path immagine= filippo.jpg
See you next !
Bookmarks