Understanding AS3 and XML
2009-10-26 00:00:00 来源:WEB开发网So the first thing to do is to create a recursive function, because we don’t know if a child of an element has its own children, that can have children, and so on. That’s why I am approaching this problem with recursive functions.
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);
for each (var item:XML in xml_to_parse.elements()) {
recursive(item);
}
}
public function recursive(xml:XML) {
trace("NAME: "+xml.name());
trace("VALUE: "+xml.children());
trace("-----------------");
}
}
}
As you can see, the result does not change…
NAME: Title
VALUE: Fly
-----------------
NAME: Description
VALUE: Simple Microban-style levels.
-----------------
NAME: Email
VALUE: bsimpson_910@hotmail.com
-----------------
NAME: LevelCollection
VALUE: <Level Id="The Fly_1" Width="11" Height="11" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<L>####</L>
<L># #</L>
<L>## ####</L>
<L>###.$.$. #</L>
<L># $.$.$ ##</L>
<L># .$@$. #</L>
<L>## $.$.{$selection}nbsp; #</L>
<L># .$.$.###</L>
<L>#### ##</L>
<L># #</L>
<L>####</L>
</Level>
<Level Id="The Fly_2" Width="13" Height="11" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<L>####</L>
<L># #</L>
<L>### ####</L>
<L>### .$.$. #</L>
<L># $.$.$ ###</L>
<L># .$#$. #</L>
<L>### $.$.{$selection}nbsp; #</L>
<L># .$.$. ###</L>
<L>#### ###</L>
<L># @#</L>
<L>####</L>
</Level>
<Level Id="The Fly_3" Width="15" Height="11" Copyright="1106 1087 Jordi Doménech" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<L>####</L>
<L>## ##</L>
<L># ###</L>
<L>#####$.$.$ ###</L>
<L># $.#.#.{$selection}nbsp; ##</L>
<L># $.@.{$selection}nbsp; #</L>
<L>## $.#.#.{$selection}nbsp; #</L>
<L>### $.$.$#####</L>
<L>### #</L>
<L>## ##</L>
<L>####</L>
</Level>
-----------------
Tags:Understanding AS and
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接