I’ve build a Array (DataListArray) and want the Array elements be able to be accessed outside the function they where created in.
The only way I can get it to work is if i do everything within the parseXMLData function.
Can sombody please help?
Thanks!
//create a new XML object
var outputXml = new XML();
//create varible for xml file url (xml url needs to be on same server)
var outputXmlUrl:String = “xmlDocs/testData1.xml”;
//ignore whitespace in the file
outputXml.ignoreWhite = true;
//load the xml file
outputXml.load(outputXmlUrl);
//call the parseXMLData function when the XML file is loaded
outputXml.onLoad = parseXMLData;
//make sure the data is loaded and usable
function parseXMLData(success:Boolean):Void {
if (success && this.status == 0) {
var DataListArray = new Array();
var DataListArray = this.firstChild.childNodes;
// populate Array ( every XML <data> Node is a new array element)
trace(DataListArray);
} else {
trace(“Problem loading XML data”);
trace("The error code is "+this.status);
}
}
// assign values to text fields
com2servTcp_txt = DataListArray[0].firstChild.firstChild.nodeValue;
com2servUpd_txt = DataListArray[1].firstChild.firstChild.nodeValue;
lineLat_txt = DataListArray[5].firstChild.firstChild.nodeValue;
lineJit_txt = DataListArray[6].firstChild.firstChild.nodeValue;
linePac_txt = DataListArray[7].firstChild.firstChild.nodeValue;
com2clTcp_txt = DataListArray[2].firstChild.firstChild.nodeValue;
com2clUpd_txt = DataListArray[3].firstChild.firstChild.nodeValue;
sipFirewall_txt = DataListArray[4].firstChild.firstChild.nodeValue;
Simply define your Array Instance outside of the function see below: Changes are in bold
//create a new XML object
var outputXml = new XML();
var DataListArray = new Array();
//create varible for xml file url (xml url needs to be on same server)
var outputXmlUrl:String = “xmlDocs/testData1.xml”;
//ignore whitespace in the file
outputXml.ignoreWhite = true;
//load the xml file
outputXml.load(outputXmlUrl);
//call the parseXMLData function when the XML file is loaded
outputXml.onLoad = parseXMLData;
//make sure the data is loaded and usable
function parseXMLData(success:Boolean):Void {
if (success && this.status == 0) {
DataListArray = this.firstChild.childNodes;
// populate Array ( every XML <data> Node is a new array element)
trace(DataListArray);
} else {
trace(“Problem loading XML data”);
trace("The error code is "+this.status);
}
}
// assign values to text fields
com2servTcp_txt = DataListArray[0].firstChild.firstChild.nodeValue;
com2servUpd_txt = DataListArray[1].firstChild.firstChild.nodeValue;
lineLat_txt = DataListArray[5].firstChild.firstChild.nodeValue;
lineJit_txt = DataListArray[6].firstChild.firstChild.nodeValue;
linePac_txt = DataListArray[7].firstChild.firstChild.nodeValue;
com2clTcp_txt = DataListArray[2].firstChild.firstChild.nodeValue;
com2clUpd_txt = DataListArray[3].firstChild.firstChild.nodeValue;
sipFirewall_txt = DataListArray[4].firstChild.firstChild.nodeValue;
That works, but I also want the populated Array accessible outside the parseXmlData function. The way it is right now the only way to assign the values to the text fields is inside the parseXmlData function.
Any ideas?
Thanks!
if you can’t get complex code to work, do it the normal basic way 
[quote=flashcb70;2329504]That works, but I also want the populated Array accessible outside the parseXmlData function. The way it is right now the only way to assign the values to the text fields is inside the parseXmlData function.
Any ideas?
Thanks![/quote]
This is because you are assigning the values within an onload event. The array values will be available outside of your parseXmlData function as long as any other function or object that needs these values makes reference to them after the the loading is complete and the array has been setup within this onload event. If you try to reference the array values outside of a conditional or the onload event the reference may fire off before the values are ready causing an Undefined error. You can easily add some error checking by setting a boolean(true/false) value once the array is setup and all values are populated. You can then use this boolean value to trigger your other functions that require the use of the array values outside of the onload event and the parseXmlData function. a simple example is below, but you could get as detailed as you want with it. New Code is in bold.
var arrayReady:Boolean = false;
outputXml.onLoad = parseXMLData;
//make sure the data is loaded and usable
function parseXMLData(success:Boolean):Void {
if (success && this.status == 0) {
DataListArray = this.firstChild.childNodes;**
arrayReady = true;
** // populate Array ( every XML <data> Node is a new array element)
trace(DataListArray);
} else {
trace(“Problem loading XML data”);
trace("The error code is "+this.status);
arrayReady = false;
}
}
// assign values to text fields
this.onEnterFrame = function() {
if(arrayReady){
com2servTcp_txt = DataListArray[0].firstChild.firstChild.nodeValue;
}else {
**** com2servTcp_txt =“sorry your array has not been populated yet”;
}
}
Sean
I also just realized that if com2servTcp_txt is a text field I am not sure how you set it up as it is not included in your code but to set the text you may want to use the following.
** com2servTcp_txt.text=
instead of
**** com2servTcp_txt=**