|
-
Problem with loading multiple external xml files
I have an array named 'source' which consists of external xml file names whose content i am trying to load into another array called 'content'. I tried with following:
Code:
.....
for (i=0; i<source.length; i++){
XML_URL = "http://localhost/temp/" + source[i];
myXMLURL = new URLRequest (XML_URL);
myLoader = new URLLoader (myXMLURL);
myLoader.addEventListener(Event.COMPLETE, xmlLoaded);
}
....
function xmlLoaded(evt:Event):void{
content.push(myLoader.data);
}
The problem is that this way i only get content of the last xml file and other 'content' elements are 'undefined'. Does anyone know solution to this problem or maybe have another way of doing this?
thanks in advance
Last edited by blu3; 10-25-2007 at 09:44 AM.
-
The problem is that by the time any of the xmls finish loading, "myLoader" is already set to the last of them. You should be able to do this instead:
Code:
function xmlLoaded(evt:Event):void{
var l:URLLoader = URLLoader(evt.currentTarget);
content.push(l.data);
}
-
First of all, thank you very much for your help. I followed your advice and modified my previous code like this:
Code:
.....
for (i=0; i<source.length; i++){
XML_URL = "http://localhost/temp/" + source[i];
myXMLURL = new URLRequest (XML_URL);
myLoader = new URLLoader (myXMLURL);
myLoader.addEventListener(Event.COMPLETE, xmlLoaded);
}
....
function xmlLoaded(evt:Event):void{
var l:URLLoader = URLLoader(evt.currentTarget);
content.push(l.data);
}
But now i get empty 'content' field so i am not sure what's going on?
-
That's weird, especially if you were getting content before. Try just target instead of currentTarget.
-
Thanks a lot for your time and help, here is the solution that finally worked:
Code:
.....
for (i=0; i<source.length; i++){
XML_URL = "http://localhost/temp/" + source[i];
myXMLURL = new URLRequest (XML_URL);
myLoader = new URLLoader ();
myLoader.addEventListener(Event.COMPLETE, xmlLoaded);
myLoader.load(myXMLURL);
}
....
function xmlLoaded(evt:Event):void{
content.push(evt.target.data);
}
cheers
-
Hello Blu3, I was looking through your post because Im having a similar problem.
Im trying to do the same as you, by pushing final data into an array.
The problem being that if I query the array after the push, it is fine, but just after the loop it is still empty and it get filled only after the entire function is run. Im new to as3, but I think Im missing some understanding of the eventListener...
.....
for (i=0; i<source.length; i++){
XML_URL = "http://localhost/temp/" + source[i];
myXMLURL = new URLRequest (XML_URL);
myLoader = new URLLoader (myXMLURL);
myLoader.addEventListener(Event.COMPLETE, xmlLoaded);
}
trace(content); // resolves empty
....
function xmlLoaded(evt:Event):void{
content.push(myLoader.data);
trace(content); // resolves fine
}
-
 Originally Posted by Litow
Hello Blu3, I was looking through your post because Im having a similar problem.
Im trying to do the same as you, by pushing final data into an array.
The problem being that if I query the array after the push, it is fine, but just after the loop it is still empty and it get filled only after the entire function is run. Im new to as3, but I think Im missing some understanding of the eventListener...
.....
for (i=0; i<source.length; i++){
XML_URL = "http://localhost/temp/" + source[i];
myXMLURL = new URLRequest (XML_URL);
myLoader = new URLLoader (myXMLURL);
myLoader.addEventListener(Event.COMPLETE, xmlLoaded);
}
trace(content); // resolves empty
....
function xmlLoaded(evt:Event):void{
content.push(myLoader.data);
trace(content); // resolves fine
}
From what i can see, it shouldn't work at all because you are missing the line where you actually load your content? You should put myLoader.load(myXMLURL) line right after your addEventListener and try then.
-
Good point. I didnt write the all bit of code.
I do have the load resolving before I make the traces.
I noticed that the
trace(content); // resolves empty
line resolves before the
trace(content); // resolves fine
one which is in the loop... that should not happen!?..
-
Another thing, from what i see you should replace
Code:
content.push(myLoader.data);
with
Code:
content.push(evt.target.data);
The rest of the code is identical to mine which worked for me so i am out of suggestions.
cheers
-
-
You're on the right track by noticing the order of the traces. The one that happens first (the empty one) happens first because loads are asynchronous. That is, they do not block execution, and things just go on their merry way. You must restructure your code so that anything that relies on the data from the loader happens after the COMPLETE event fires. The easy way to do that is to make it happen as part of a listener for that event.
I'll sketch up some pseudocode.
before (bad):
Code:
.....
for (i=0; i<source.length; i++){
XML_URL = "http://localhost/temp/" + source[i];
myXMLURL = new URLRequest (XML_URL);
myLoader = new URLLoader (myXMLURL);
myLoader.addEventListener(Event.COMPLETE, xmlLoaded);
}
trace(content); // resolves empty
....<more stuff that uses content>
function xmlLoaded(evt:Event):void{
content.push(myLoader.data);
trace(content); // resolves fine
}
After restructuring (better):
Code:
var loadedCount:int = 0;
.....
for (i=0; i<source.length; i++){
XML_URL = "http://localhost/temp/" + source[i];
myXMLURL = new URLRequest (XML_URL);
myLoader = new URLLoader (myXMLURL);
myLoader.addEventListener(Event.COMPLETE, xmlLoaded);
}
trace(content); // resolves empty
....
function xmlLoaded(evt:Event):void{
content.push(myLoader.data);
trace(content); // resolves fine
loadedcount++;
if (loadedcount >= source.length){
continueLogic();
}
}
private function continueLogic():void{
<more stuff that uses content>
}
Note a few things: 1)Things will not necessarily load in order, so don't depend on content's content to be in the same order as source's
2) because you started multiple loaders in parallel, I had to add a variable to keep track of how many had completed so we know when it's safe to continue. This means loadedCount, source, and content must be class level.
-
That worked great! Right on the spot.
Thank you Sir
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|