类示例:
package {
import flash.events.*;
import flash.net.*;
public class XmlLoader extends EventDispatcher{
private var _xmlList:XMLList;
public function XmlLoader(xmlPath:String){
var xmlUrlRequest:URLRequest = new URLRequest(xmlPath);
var xmlUrlLoader:URLLoader = new URLLoader(xmlUrlRequest);
xmlUrlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
}
private function xmlLoaded(e:Event):void{
e.currentTarget.removeEventListener(Event.COMPLETE, xmlLoaded);
var xml:XML = new XML(e.currentTarget.data);
this._xmlList = xml.*;
dispatchEvent(new Event(Event.COMPLETE));
}
public function getXmlList():XMLList{
return this._xmlList;
}
}
}
类使用的一个例子是:
var menuXmlList:XmlLoader = new XmlLoader("pt/menu.xml");
menuXmlList.addEventListener(Event.COMPLETE, printList);
function printList(e:Event):void{
trace(e.currentTarget.getXmlList());
}
这将打印出 xml 文件的内容。如何在类外访问 xmlList 的节点?