as 3.0解析xml文档
今天看到一个同学写的现在贴出来希望给各位一些帮助.先我们看看xml 文档的格式,
<?xml version="1.0" encoding="utf-8"?>
<data>
<img>
<path>"img/11.jpg"</path>
<txt>"这是第一张人物图"</txt>
</img>
<img>
<path>"img/11.jpg"</path>
<txt>"这是第二张人物图"</txt>
</img>
<img>
<path>"img/11.jpg"</path>
<txt>"这是第三张人物图"</txt>
</img>
<img>
<path>"img/11.jpg"</path>
<txt>"这是第四张人物图"</txt>
</img>
</data>
这是一个很简单的xml文件,下在我们用as3.0来读出我们想要的内容.
package {
import flash.display.Sprite;
import flash.net.*;
import flash.events.*;
import flash.utils.Timer;
public class XMLRequest extends EventDispatcher {
private var myArray:Array=new Array();
private var requXML:XML;
private var pathtxt:String;
private var ttxt:String;
private var loader:URLLoader;
private var requ:URLRequest;
public function XMLRequest() {
loader=new URLLoader();
requ=new URLRequest("xx.xml");
listener(loader);
try {
loader.load(requ);
} catch (error:Error) {
trace("Unable to load requested document.!");
}
trace("readXML.end....",myArray);
}
public function readXML():Array {
return myArray;
}
private function listener(lo:IEventDispatcher):void {
lo.addEventListener(ProgressEvent.PROGRESS,progerss);
lo.addEventListener(Event.COMPLETE,loadcomplete);
}
private function loadcomplete(event:Event):void {
requXML=new XML(event.target.data);
for (var k in requXML..img) {
pathtxt=requXML..img[k].path;
ttxt=requXML..img[k].txt;
myArray.push(pathtxt,ttxt);
}
dispatchEvent(new Event(Event.COMPLETE));
}
private function progerss(event:ProgressEvent):void {
//trace( "333progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal);
}
}
}
补充:flash教程,As3.0