Depending on what you're trying to achieve, you may not need an 
Array/ArrayCollection and can use the XML/XMLList as is.
Lets say you want to populate a DataGrid with your xml string, then the 
following will work just fine:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"; layout="absolute" 
creationComplete="initApp();">

 <mx:Script>
  <![CDATA[
   import mx.collections.ArrayCollection;
   private var dummy:String = "<items>" +
     "<item>" +
     "<Month>January</Month><Sales>1200</Sales><People>5</People>" +
     "</item>" +
     "<item>" +
     "<Month>February</Month><Sales>1000</Sales><People>3</People></item>" +
     "</items>";
   //-------------------------------------------------------
   //
   //-------------------------------------------------------
   private function initApp():void {
    trace("Application ::: initApp");
    // convert string to xml
    var _xml:XML = new XML(dummy);
    trace(_xml.toXMLString());
    trace("-----------------------------------");
    // get xml children (elements)
    var elements:XMLList = _xml.elements();
    trace("    - elements length: "+elements.length());
    // access/use elements as array
    trace("    - month: "+elements[0].Month);
    trace("    - sales: "+elements[0].Sales);
    trace("    - people: "+elements[0].People);
    // set XMLList as dataprovider for datagrid
    _dg.dataProvider = elements;
   }
  ]]>
 </mx:Script>

 <mx:DataGrid id="_dg">
  <mx:columns>
   <mx:DataGridColumn dataField="Month" />
   <mx:DataGridColumn dataField="Sales" />
   <mx:DataGridColumn dataField="People" />
  </mx:columns>
 </mx:DataGrid>

</mx:Application>

regards,
Muzak

----- Original Message ----- 
From: "Private Romeo" <[EMAIL PROTECTED]>
To: <flexcoders@yahoogroups.com>
Sent: Monday, February 19, 2007 5:46 PM
Subject: [flexcoders] Question regarding Array creation from XML


> Assume I've got the following String variable:
>
>
>
> var dummyXml:String =
> "<items><item><Month>January</Month><Sales>1200</Sales><People>5</People></i
> tem><item><Month>February</Month><Sales>1000</Sales><People>3</People></item
>></items>";
>
>
>
> What is the easiest way to turn it into an Array/ArrayCollection?
>
>
>
>
>
> 


Reply via email to