5 Replies Latest reply on Feb 4, 2007 6:21 PM by injpix

    Returning an array of objects from a function

    shaunie123
      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[n] = 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[n].firstChild.firstChild.nodeValue;
      var var2:String = myXML.firstChild.childNodes[n].firstChild.nextSibling.firstChild.nodeValue;
      var var3:String = myXML.firstChild.childNodes[n].firstChild.nextSibling.nextSibling.firstChild.nodeValue;
      var var4:String = myXML.firstChild.childNodes[n].firstChild.nextSibling.nextSibling.nextSibling.firstChild. nodeValue;
      var var5:String = myXML.firstChild.childNodes[n].firstChild.nextSibling.nextSibling.nextSibling.nextSibling .firstChild.nodeValue;
      var var6:String = myXML.firstChild.childNodes[n].lastChild.firstChild.nodeValue;
      var myRow:Object = {Var1:var1,
      Var2:var2,
      Var3:var3,
      Var4:var4,
      Var5:var5,
      Var6:var6};
      list.addItem(myRow);
      }
      return list;
      }