Question
Returning an array of objects from a function
Hi,
I am trying to return an array of objects from a function that reads in an xml file. Everytime I try to do a trace on populationData I get undefined.
I have a similar function that returns an array of values that works fine i.e.
list = myValue; instead of list.addItem(myRow);
but when i try to create an array of objects it doesnt work, if anyone could tell where I am going wrong I would be most grateful, here is my code:
/**
* Load imported xml values into the application variables
*/
loadXMLValues = function(file) : Void {
var populationData = new Array();
var myXML:XML = new XML();
myXML.ignoreWhite = true;
myXML.onLoad = function(success) {
if (success) {
populationData = importData(myXML);
trace(populationData);
} else {
// Error
}
}
myXML.load(file);
}
/**
* Return an array of demographic values from xml file
*/
importData = function(myXML): Array {
var list:Array = new Array();
for (n=0; n<myXML.firstChild.childNodes.length; n++) {
var var1:String = myXML.firstChild.childNodes.firstChild.firstChild.nodeValue;
var var2:String = myXML.firstChild.childNodes.firstChild.nextSibling.firstChild.nodeValue;
var var3:String = myXML.firstChild.childNodes.firstChild.nextSibling.nextSibling.firstChild.nodeValue;
var var4:String = myXML.firstChild.childNodes.firstChild.nextSibling.nextSibling.nextSibling.firstChild.nodeValue;
var var5:String = myXML.firstChild.childNodes.firstChild.nextSibling.nextSibling.nextSibling.nextSibling.firstChild.nodeValue;
var var6:String = myXML.firstChild.childNodes.lastChild.firstChild.nodeValue;
var myRow:Object = {Var1:var1,
Var2:var2,
Var3:var3,
Var4:var4,
Var5:var5,
Var6:var6};
list.addItem(myRow);
}
return list;
}
I am trying to return an array of objects from a function that reads in an xml file. Everytime I try to do a trace on populationData I get undefined.
I have a similar function that returns an array of values that works fine i.e.
list
but when i try to create an array of objects it doesnt work, if anyone could tell where I am going wrong I would be most grateful, here is my code:
/**
* Load imported xml values into the application variables
*/
loadXMLValues = function(file) : Void {
var populationData = new Array();
var myXML:XML = new XML();
myXML.ignoreWhite = true;
myXML.onLoad = function(success) {
if (success) {
populationData = importData(myXML);
trace(populationData);
} else {
// Error
}
}
myXML.load(file);
}
/**
* Return an array of demographic values from xml file
*/
importData = function(myXML): Array {
var list:Array = new Array();
for (n=0; n<myXML.firstChild.childNodes.length; n++) {
var var1:String = myXML.firstChild.childNodes
var var2:String = myXML.firstChild.childNodes
var var3:String = myXML.firstChild.childNodes
var var4:String = myXML.firstChild.childNodes
var var5:String = myXML.firstChild.childNodes
var var6:String = myXML.firstChild.childNodes
var myRow:Object = {Var1:var1,
Var2:var2,
Var3:var3,
Var4:var4,
Var5:var5,
Var6:var6};
list.addItem(myRow);
}
return list;
}