views:

24

answers:

2

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

}

A: 

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?

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

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.

MichaelJW
ah right.....silly me XDthanx!
Hwang
you're welcome :)
MichaelJW