var xml:XML;
var urlL:URLLoader = new URLLoader();
var xmlArray:Array = new Array();
var i:uint;
urlL.dataFormat = URLLoaderDataFormat.TEXT;
urlL.addEventListener(Event.COMPLETE, onLoadedAction);
urlL.load(new URLRequest("list.xml"));
function onLoadedAction(e:Event):void {
try {
xml = new XML(e.target.data);
xml.ignoreWhitespace = true;
for (i = 0; i<xml.video.length(); i++) {
xmlArray.push(xml.video.path[i]);
//trace(xmlArray[i]);
}
} catch (e:Error) {
trace(e.message);
}
}
trace(xmlArray[0]);
这是我的代码。 当我从数组中追踪第0个索引值时,我在输出面板中得到“未定义”。
错误是什么?
答案 0 :(得分:2)
从提供的代码中,看起来好像是在XML有机会完全加载(在onLoadedAction函数之外)之前尝试跟踪该值。如果将跟踪移动到onLoadedAction函数的结尾(内部),则应该看到结果。
答案 1 :(得分:1)
//Imports
import flash.errors.IOError;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
//Variables
var xml:XML;
var xmlLoader:URLLoader = new URLLoader();
var xmlArray:Array = new Array();
//Load XML
xmlLoader.addEventListener(IOErrorEvent.IO_ERROR, xmlLoaderErrorEventHandler);
xmlLoader.addEventListener(Event.COMPLETE, xmlCompleteEventHandler);
xmlLoader.load(new URLRequest("list.xml"));
//XML Loader Error Event Handler
function xmlLoaderErrorEventHandler(evt:IOErrorEvent):void
{
throw new IOError(evt.text);
}
//XML Complete Event Handler
function xmlCompleteEventHandler(evt:Event):void
{
evt.currentTarget.removeEventListener(IOErrorEvent.IO_ERROR, xmlLoaderErrorEventHandler);
evt.currentTarget.removeEventListener(Event.COMPLETE, xmlCompleteEventHandler);
xml = new XML(evt.currentTarget.data);
for each (var element:XML in xml.video.path)
xmlArray.push(element);
trace(xmlArray);
}
答案 2 :(得分:0)
尝试更改此内容:
xmlArray.push(xml.video.path[i]);
对此:
xmlArray.push(xml.video..path[i]);