PDA

View Full Version : XML and AS2.0



orange_01
March 9th, 2005, 10:18 PM
Hey all,

I have this method within a class. It loads in an XML file, then attempts to call another function. For some reason however, the loadXML() does not execute. If I put a trace right before it, the trace executes. If I take that function out of the onload, it executes.

private function openXML (URL:String) {
var xmlData:XML = new XML();
xmlData.ignoreWhite = true;
xmlData.load(URL);
xmlData.onLoad = function () {
loadXML();
}
}

My overall problem is that I am trying to setup two methods, one to load in the XML, the other to populate an array with its contents. But I am unable to keep the next action from happening before the XML is done loading.

I've also tried doing the array loading procedures in the onload, then passing the loaded array out, but the return statement executes before the contents of the onload success function.

I've even tried having the loadXML function set a flag, and have the constructor sit in a while loop until it saw the flag go up, but it produces an infinite loop.

class XMLLoader {

private var loadFinished:Boolean = false;

// The constructor funtion
public function XMLLoader (URL:String) {

openXML(URL);
while (loadFinished == false) {
checkLoaded();
}
if (loadFinished == true) {
loadXML();
}

}

private function openXML (URL:String) {
var xmlData:XML = new XML();
xmlData.ignoreWhite = true;
xmlData.load(URL);
xmlData.onLoad = function () {
loadFinished = true;
}
}

private function checkLoaded ():Boolean {
if (loadFinished == false) {
return false;
} else {
return true;
}
}

private function loadXML (URL:String) {
trace("Did it");
}
}

I've been at this for 12 hours straight, any suggestions? I feel like I've tried everything. Could a listener work in this situation, if so how?

orange_01

Gompje
March 10th, 2005, 03:08 PM
Hello,

I've had the exact same problem this afternoon ;)

ok in short, this is what you have to do:

1) You need to use the new Delegate methods (found in mx.utils.Delegates) so you can redirect the scoop of the "onload" function. This enables you to call an function inside the class instance, rather than something in the "xml" object you've created. edit[Kirupa has an article on this but I don't remember the link for now.] http://www.kirupa.com/web/xml/XMLspecificIssues3.htm

eg: _xml.onLoad = Delegate.create(this, onLoadEvent);

where onLoadEvent is you custom function (name can be anything of course)

2) this is not exactly a "Must do" but I advice you to do it: create an custom event in your class and triggers it when everything is nicely done and filled. I found out that in Flash things seems to execute in a strange order than I'm used to in java,.NET,etc.
just read http://www.macromedia.com/devnet/mx/flash/articles/creating_events.html

so I hope I could help you

// I did not analyse your code snip completely, but if you want to I will :)