0

I can I get the XML data from 3rd function?

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 Answers 2

2

It will take some time for the loadData() function to load the XML file, and then put this data into myXML. But 3rdfunction() is run immediately after loadData(), which means there won't have been enough time for myXML to have been loaded when you try to trace it.

To fix this, you could move the 3rdfunction() call to 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);

This way, 3rdfunction() will only be run after data.xml has been loaded into your myXML object, so myXML should definitely contain something.

Sign up to request clarification or add additional context in comments.

Comments

0

If you just want to trace the contents of myXML, try:

trace(myXML.toXMLString());

Or did you want to do more with the data, like actually parse it with code?

3 Comments

well technically i need to able to trace it before i could do anything else with it right?
Ah, I think I misread your question. Are you asking why 3rdfunction() isn't tracing anything yet?
yup. Any idea how to overcome the problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.