PDA

View Full Version : need help with xml...



sasxa
June 4th, 2007, 09:52 PM
What's wrong with this code? Why is my xml variable null in start() function?

Tnx, Sasxa



package {
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLRequest;

public class MyClass {
private var xml:XML;

public function MyClass() {

var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, loadXML);
xmlLoader.load(new URLRequest("test.xml"));

start();
}

private function loadXML(event:Event):void {
xml = new XML(event.target.data);
trace(xml); // works here...
}

private function start():void {
trace(xml); // null here???
}
}
}

dthought
June 4th, 2007, 10:27 PM
The XML file has not loaded when you immediately call the method start(), therefore it takes the default value of an XML instance when it contains no data - that is, "null". In AS2.0, it would have returned "undefined".

It works in the loadXML method, because the XML has been loaded into memory a few frames after it was called.

Loading, even locally, is not instantaneous and you cannot expect it to be so. Hence why we have event listeners to tell us when things are ready :)

sasxa
June 4th, 2007, 10:46 PM
Tnx,
can't believe I missed that (: