Understanding AS3 and XML
2009-10-26 00:00:00 来源:WEB开发网This is one of the worst XML you can find because it has an unknown number of nodes (I don’t know how many levels has a level collection) and some nodes with multiple attributes.
But you’ll see parsing it with AS3 will be very easy anyway.
Let’s see how to read the xml file:
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
import flash.net.URLLoader;
public class sokoxml extends Sprite {
public function sokoxml() {
var loader:URLLoader=new URLLoader;
var req:URLRequest=new URLRequest("sokobanxml.xml");
loader.addEventListener(Event.COMPLETE,on_xml_completed);
loader.load(req);
}
public function on_xml_completed(event:Event):void {
var xml_to_parse:XML=new XML(event.target.data);
trace(xml_to_parse)
}
}
}
This would outputs the entire XML file in the same way it was created. Let’ see how does it work:
Lines 8-9: Setting the XML file to load
Line 10: Adding a listener to check if the XML has been completely loaded
Line 11: Loading the file
Line 14: Transforming the file in a XML variable. Not it’s ready to be parsed
The first things we want to look for in an XML file are elements. The elements method lists the elements of an XML object.
Tags:Understanding AS and
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接