从第三个函数获取XML数据?

时间:2009-10-14 14:08:23

标签: xml actionscript-3

我可以从第3个函数中获取XML数据吗?

package {
import flash.display.*;
import flash.events.*;
import flash.net.*;

public class main extends MovieClip {
    private var myXML:XML;
    private var myXMLlist:XMLList;
    private var myLoader:URLLoader = new URLLoader();

    public function main():void {
        loadData();
        3rdfunction();
    }

    private function loadData():void {
        myLoader.load(new URLRequest("data.xml"));
        myLoader.addEventListener(Event.COMPLETE, processXML);
    }
    private function processXML(e:Event):void {
        myXML=new XML(e.target.data);
        trace(myXML.length())
    }


    private function 3rdfunction():void {

        trace(myXML);

}

2 个答案:

答案 0 :(得分:2)

loadData()函数需要一些时间才能加载XML文件,然后将这些数据放入myXML中。但是在loadData()之后立即运行3rdfunction(),这意味着当你试图跟踪它时,没有足够的时间来加载myXML。

要解决此问题,您可以将3rdfunction()调用移至processXML():

public class main extends MovieClip {
    private var myXML:XML;
    private var myXMLlist:XMLList;
    private var myLoader:URLLoader = new URLLoader();

    public function main():void {
            loadData();
    }

    private function loadData():void {
            myLoader.load(new URLRequest("data.xml"));
            myLoader.addEventListener(Event.COMPLETE, processXML);
    }
    private function processXML(e:Event):void {
            myXML=new XML(e.target.data);
            trace(myXML.length())
            3rdfunction();
    }


    private function 3rdfunction():void {

            trace(myXML);

这样,只有在将data.xml加载到myXML对象后才会运行3rdfunction(),所以myXML肯定会包含一些内容。

答案 1 :(得分:0)

如果您只想跟踪myXML的内容,请尝试:

trace(myXML.toXMLString());

或者你想对数据做更多的事情,比如用代码实际解析它?