This is one of those things that took me much longer than I thought it would have. I needed to convert a file containing XML data to an ArrayCollection. My XML file looked like this:
<rows> <row> <col1>some value</col1> <col2>another value</col2> ... </row> ... </rows>
I wanted to convert this into an ArrayCollection with each row as an object (and I didn’t want to have to loop through each item in the list). Here’s the solution I’m using.
import mx.utils.ArrayUtil;
import mx.rpc.xml.SimpleXMLDecoder;
import mx.collections.ArrayCollection;
private function convertXmlToArrayCollection( file:String ):ArrayCollection
{
var xml:XMLDocument = new XMLDocument( file );
var decoder:SimpleXMLDecoder = new SimpleXMLDecoder();
var data:Object = decoder.decodeXML( xml );
var array:Array = ArrayUtil.toArray( data.rows.row );
return new ArrayCollection( array );
}
I’m not thrilled with this approach because I’ve needed to hard code the XML structure (’rows.row’) into the function. If anyone has a better solution I’d love to see it.
Best,
Hillel