AS3/XML - return loaded XML data?

I’ve gone through the tutorial (very helpful, thanks Kirupa), but I have a question about the xmlLoader function on page 2.

Here’s how I have it implemented at the moment…



package {

    import flash.events.*;
    import flash.net.*;
    
    internal function XMLLoader(pathToXML:String):void {
        var xmlLoader:URLLoader = new URLLoader();
        var xmlData:XML = new XML();
        xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
        xmlLoader.load(new URLRequest(pathToXML));
        function LoadXML(e:Event):void {
            xmlData = new XML(e.target.data);
            trace("loaded:");
            trace(xmlData);
        }
    }
}

The problem comes when I try to set this up so it returns an XML file, i.e.


var my_xml:XML = XMLLoader("test.xml");

I set the return type to xml instead of void, but simply putting

return(xmlData);

at the end of the XMLLoader function doesn’t work. It attempts to return the variable before it’s actually loaded, sending back a null value.

I’m new to AS3 and only at a basic level of OOP, so I’m probably going about this the entirely wrong way. (I’ve also tried the class included in Colin Moock’s ‘Essential Actionscript 3,’ but was unable to get it to work). Any ideas?

I’d like to do it like this because I have several XML files to load and would rather not have to duplicate the code.

Thanks in advance to anyone who can help.