Understanding AS3 and XML
2009-10-26 00:00:00 来源:WEB开发网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()) {
trace(item.name()+": "+item+"\n--------------");
}
}
}
}
With the for each loop we can parse all elements even if we do not know how many of them we have in our XML file. The output is pretty similar to the original file because elements method does not care if an element has children.
Title: Fly
--------------
Description: Simple Microban-style levels.
--------------
Email: bsimpson_910@hotmail.com
--------------
LevelCollection: <LevelCollection Copyright="Tim LeFevre" MaxWidth="15" MaxHeight="11" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Level Id="The Fly_1" Width="11" Height="11">
<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">
<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">
<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>
</LevelCollection>
--------------
Tags:Understanding AS and
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接