I’ve been wrestling with this for approximately 8 hours now with no luck.
I have an XML object, which, after being loaded with an external XML file, I would like to traverse through and place values from the XML file into an array. When the traversing is complete I need to fire an event so that the rest of the program can continue.
The problem is that the onLoad seems to be firing before the XML file is actually loaded. I’ve worked up some sample code that demonstrates what is happening.
XMLReaderTest class
import mx.events.EventDispatcher
class XMLReaderTest {
private var myArray:Array;
private var xmlObj:XML;
function XMLReaderTest() {
this.xmlObj = new XML();
this.xmlObj.ignoreWhite = true;
this.myArray = new Array();
mx.events.EventDispatcher.initialize(this);
}
function dispatchEvent() {};
function addEventListener() {};
function removeEventListener() {};
function readXML(xmlFile:String):Void{
this.xmlObj.onLoad = loadArray();
this.xmlObj.load(xmlFile);
}
function loadArray(){
var eventObject:Object = {target:this, type:'arrayLoaded'};
var temp = new Array();
temp = this.xmlObj.childNodes[0].childNodes;
//remove any disabled items
for (var p = 0; p<temp.length; ++p) {
if (temp[p].attributes["enabled"].toString() == "false") {
temp.splice(p, 1);
}
}
//add to array
for(var i=0; i<temp.length; i++){
var myVal = temp*.childNodes[0].firstChild.nodeValue.toString();
this.myArray.push(myVal);
}
dispatchEvent(eventObject);
}
}
Main Timeline:
var myXMLReader = new XMLReaderTest();
var myListnerObj:Object = new Object;
myListnerObj.arrayLoaded = function(evtObj) {
trace("The array is loaded.");
}
myXMLReader.addEventListener("arrayLoaded",myListnerObj);
myXMLReader.readXML("mytest.xml");
and the XML file (myTest.xml):
<?xml version="1.0" encoding="ISO-8859-1"?>
<myTest>
<anItem>
<value>1</value>
</anItem>
<anItem>
<value>3</value>
</anItem>
<anItem>
<value>2</value>
</anItem>
</myTest>
Does anyone have any idea why this is happening, or how I can fix it? I really appreciate all the help I can get.
Thanks,
adam