bzouchir
August 5th, 2007, 10:29 PM
Hi everyone,
I'm just trying to write an XMLLoader class, that I can use over and over, and passing a string of the xml url to load.
the XMLLoader class is:
package classes
{
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
public class XMLLoader extends Sprite
{
private var _xmlData:XML;
private var loader:URLLoader;
public function XMLLoader(xmlURL:String)
{
var xmlRequest:URLRequest = new URLRequest(xmlURL);
loader = new URLLoader (xmlRequest);
loader.addEventListener(Event.COMPLETE, onComplete);
trace("XML Loader init()");
}
private function onComplete(event:Event):void
{
_xmlData = XML(loader.data);
trace("xml is loaded");
}
public function get xmlData():XML
{
var _data:XML = _xmlData;
return _data;
}
}
}
the main class is XMLSetup :
package classes
{
import flash.display.Sprite;
import flash.events.Event;
public class XMLSetup extends Sprite
{
private var loader:XMLLoader;
private var xmlURL:String = "xml/test.xml";
public function XMLSetup()
{
init();
}
private function init():void
{
loader = new XMLLoader(xmlURL);
trace(loader.xmlData);
trace("XMLSetup init()");
}
}
}
now when I run a test it outputs:
XML Loader init()
null
XMLSetup init()
xml is loaded
so basically trace(loader.xmlData);
is being called before the xml is loaded; returning null.
what is the best way of fixing this? is there any event that I can fire for the main class to know that intance has loaded the xml? aor what is the best way to do this?
thanks
I'm just trying to write an XMLLoader class, that I can use over and over, and passing a string of the xml url to load.
the XMLLoader class is:
package classes
{
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
public class XMLLoader extends Sprite
{
private var _xmlData:XML;
private var loader:URLLoader;
public function XMLLoader(xmlURL:String)
{
var xmlRequest:URLRequest = new URLRequest(xmlURL);
loader = new URLLoader (xmlRequest);
loader.addEventListener(Event.COMPLETE, onComplete);
trace("XML Loader init()");
}
private function onComplete(event:Event):void
{
_xmlData = XML(loader.data);
trace("xml is loaded");
}
public function get xmlData():XML
{
var _data:XML = _xmlData;
return _data;
}
}
}
the main class is XMLSetup :
package classes
{
import flash.display.Sprite;
import flash.events.Event;
public class XMLSetup extends Sprite
{
private var loader:XMLLoader;
private var xmlURL:String = "xml/test.xml";
public function XMLSetup()
{
init();
}
private function init():void
{
loader = new XMLLoader(xmlURL);
trace(loader.xmlData);
trace("XMLSetup init()");
}
}
}
now when I run a test it outputs:
XML Loader init()
null
XMLSetup init()
xml is loaded
so basically trace(loader.xmlData);
is being called before the xml is loaded; returning null.
what is the best way of fixing this? is there any event that I can fire for the main class to know that intance has loaded the xml? aor what is the best way to do this?
thanks