我正在尝试使用这个xml数据:
http://flydata.avinor.no/XmlFeed.asp?TimeFrom=1&TimeTo=10&airport=KSU&direction=D%22
它包含来自机场的所有官方飞行数据。我想得到所有航空公司的名字。
无论我尝试什么,我都会得到NULL。如果我试图找出标签的长度,我得到0。
这是我的代码;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
var myXML:XML = new XML();
var XML_URL:String = "http://flydata.avinor.no/XmlFeed.asp?TimeFrom=1&TimeTo=10&airport=KSU&direction=D%22";
var re:URLRequest = new URLRequest(XML_URL);
var ldr:URLLoader = new URLLoader(re);
ldr.addEventListener(Event.COMPLETE, loaded);
function loaded(e:Event):void{
myXML = XML(ldr.data);
trace("Succses");
}
var howLong:int = myXML.flight.length();
for(var count:int=0; count < howLong; count++){
var airline:String = myXML.flight[count].airline;
}
trace(airline)
答案 0 :(得分:0)
URLRequest
是一个异步操作,因此在加载XML之前执行for
循环。您需要将遍历返回的XML的代码移动到完整的处理程序中:
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
var myXML:XML = new XML();
var XML_URL:String = "http://flydata.avinor.no/XmlFeed.asp?TimeFrom=1&TimeTo=10&airport=KSU&direction=D%22";
var re:URLRequest = new URLRequest(XML_URL);
var ldr:URLLoader = new URLLoader(re);
ldr.addEventListener(Event.COMPLETE, loaded);
function loaded(e:Event):void {
var howLong:int
var airline:String;
myXML = XML(ldr.data);
howLong = myXML.flight.length();
trace("Succses");
for(var count:int=0; count < howLong; count++){
airline = myXML.flight[count].airline;
trace(airline);
}
}