read xml file?

I have tried to use the xml loader but it doesn't work as I suspected.
I want to write a program that does the following:
1. I want to read data from a xml file and put some of the data into an array.
2. I want to initialize some other array and variables.
3. I want to use the data that I stored in the array when the data was loaded from the xml file
4. Then I want to move around objects with parameters that I got in the xml file.

My problem is that it seems that the xml loader takes a little bit to long time or something else is very wrong.
In the code below I read the xml file and call a function loadXML, I use a trace in this just to see when it executes.
After the loader declaration with its eventlistener I want to start using the array that is supposed to be filled in the loadXML function. But its here my prblem occurs. The data isn't filled in when I want to use it, in the trace that I use below it says that my array has length 0 "trace(rutArray.length);" and then after that trace I receive the other trace "trace(xml.RutCoord[1].xSize);"

So it seems that the code continue to execute or excutes before the loaders eventlistener occurs.

Can someone please help me with this problem. I don't want to move all my code into the xmlLoad function because I'm going to add more loaders that reads other files later on and I will get a very nasty code if I start adding code into the load function.

It also seems like a terrible idea to add some sort of delay with a timer, I just want to wait until the function xmlLoad is finished until I continue to execute the rest of the code.


var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, loadXML);
loader.load(new URLRequest("rutCoord.xml"));
var rutArray:Array =new Array();
var xml:XML;
function loadXML(e:Event):void
{
xml = new XML(e.target.data);
trace(xml.RutCoord[1].xSize);
for (var i:uint=0; i < xml.RutCoord.length(); i++) {
var tmpObject:Object = new Object();
tmpObject.xSize = xml.RutCoord[i].xSize;
rutArray.push(tmpObject);
}

}
trace(rutArray.length);
I'm not sure your e.target.data is going to give you the correct type to access .data, you might want to trace e.target.data and see what it gives you, it should display everything in the xml. If that is the problem then consider trying

var myLoader = e.target as URLLoader
xml:XML = new XML(myLoader.data);

Alternatively for a simple fix just use the reference to the publicly known Loader

var xml:XML = new XML(loader.data);

Original thread: https://box2d.org/forum/viewtopic.php?t=546