开发者

Problem in getting values from an array

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]开发者_StackOverflow社区);
        }
    } catch (e:Error) {
        trace(e.message);
    }
}
trace(xmlArray[0]);

This is my code. When I am tracing the 0 th index value from the array, I am getting "undefined" in the output panel.

What is the bug?


From the code provided, it looks as though you're attempting to trace the value before the XML has a chance to completely load (outside of the onLoadedAction function). If you move your trace to the end (inside) of the onLoadedAction function, you should see a result.


//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);
}


Try changing this:

xmlArray.push(xml.video.path[i]);

To this:

xmlArray.push(xml.video..path[i]);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜