Reputation: 3034
I'm trying to load an XML file with a list of books, I looked around and found this function, tried to adjust it to work for me, but I cant get this to work, I'm able to load the XML and actually read the info from it, but I was trying to set a global array or something like that so I can access the data later,
heres the code:
var books:XML = loadBooks();
trace(books); //Returns a blank output
function loadBooks():XML {
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("bookList.xml"));
function LoadXML(e:Event):void {
xmlData = new XML(e.target.data);
trace(xmlData); //Returns what I want to have
}
return xmlData;
}
I added comments on the trace() parts to explain a little about what's happening Thanks in advance.
Upvotes: 1
Views: 1930
Reputation: 165
At the initial stage you're trying to trace books, the loader hasn't finished loading in your XML file - so it will not trace anything. The second one however will because it is tied to the Complete event and will only be fired when the loader has finished loading the XML. So as above, anything you want to do with your XML file should be placed inside the LoadXML function.
Upvotes: 0
Reputation: 2367
The first trace does not show what you want, because the XML is loaded asynchronously. This is why you add the LoadXML
as an event listener for the "complete" event. So, whatever you want to do with the loaded XML, you should do it in the LoadXML
function.
Upvotes: 1