views:

150

answers:

1

I have an application which receives dynamic XML data from a server. The structure of the XML is dynamic and the tags/attribute names cannot be predicted. NO row items can be hardcoded. The data is coming back from a database (imagine columns and rows), and the type of the data is known on the server side. The following is just an example, an only shows the structure of how the data comes back.

<dataset>
    <row>
        <firstName value="Chris" type="String"/>
        <lastName value="McDonald" type="String"/>
        <age value="24" type="Integer"/>
    </row>
    <row>
        <firstName value="Bob" type="String"/>
        <lastName value="Bourne" type="String"/>
        <age value="43" type="Integer"/>
    </row>
</dataset>

I am wondering how/if I can get that data into typed values in an arraycollection like this:

public var arr:ArrayCollection = new ArrayCollection(
    [firstName:"Chris", lastName:"McDonald", age:24], ...); // Note that age is an integer, not a string

Thanks in advance

+2  A: 

Yes I think you can do that. See if this helps:

 public function parseXML(datasetXML:String):ArrayCollection {
  var a:Array = new Array();
  var xml:XML = new XML(datasetXML);
  xml.ignoreWhitespace = true;
  var rows:XMLList = xml.row;
  for each (var row in rows) {
    a.push(getObject(row));
  }
}
public function getObject(xml:XML):Object {
  var obj:Object = new Object();
  for each (var column in xml.elements()) {
    if (column.@type == "String") {
      obj[column.localName()] = column.@value;
    }else if(column.@type = "Integer") {
      obj[column.localName()] = int(column.@value);
    }
  }
  return obj;
}

bhups