PDA

View Full Version : XMLList to an array collection


corbo950
07-29-2008, 10:28 PM
how do i convert XML or an XMLList to an arraycollection?

lordofduct
07-30-2008, 12:14 AM
It kinda depends on your structure. And XML and XMLList are enumerable as well... so umm, could you expand on this a little bit?

ryosaeb4
01-26-2009, 09:19 AM
I've the same problem...with a my custom Component

the code in my custom popUp that extends TitleWindows is:

<?xml version="1.0" encoding="utf-8"?>
<mx:TitleWindow xmlns:mx=" http://www.adobe.com/2006/mxml " layout="absolute" width="300" height="250" title="PopUp Info"
showCloseButton="true" close=" PopUpManager.removePopUp(this )" horizontalAlign="center" verticalAlign="middle" creationComplete=" unitRPC2.send( )">

<mx:Script>
<! CDATA[
import [mx.rpc.events.FaultEvent ;
import mx.managers.PopUpManager ;
import flash.events.MouseEvent ;
import mx.controls.* ;
import flash.net.FileReference ;
import mx.rpc.events.ResultEvent ;
import mx.collections.ArrayCollection ;

[Bindable]
public var idD:String;

[Bindable]
private var unitsDataGrid:ArrayCollection;

private function doLogin(e:MouseEvent):void {
PopUpManager.removePopUp(this );
trace( this.idD );
}



private function unitRPCResultXML(event:ResultEvent):void
{
unitsDataGrid = event.result.Items.Item.info ;
trace(unitsDataGrid);
}

private function faultHandler(event:FaultEvent):void{
var faultMessage:String = "Non posso connettermi al file XML";
Alert.show(faultMessage , "Errore nell'apertura del file");
}


]]>
</mx:Script>


<mx:HTTPService id="unitRPC2" url="assets/prova_popup_info.xml" result="unitRPCResultXML(event)" fault="faultHandler(event)" resultFormat="e4x"/>

<mx:Label id="labelUSER" text="{idD}" x="10" y="182" width="133"/>
<mx:DataGrid id="dataGrid" dataProvider="{unitsDataGrid}" x="10" y="10" width="260" height="164" />
</mx:TitleWindow>


the XML is very easy:
<?xml version="1.0" encoding="utf-8"?>
<ItemLookupResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://webservices.amazon.com/AWSECommerceService/2005-10-05">
<Items>
<Item>
<ItemAttributes>
<Title>infoLand</Title>
<ASIN>6</ASIN>
</ItemAttributes>
<SmallImage>

<URL>manager.png</URL>
</SmallImage>
<SimilarProducts>
<SimilarProduct>
<ASIN>3</ASIN>
<Title>Portale Comuni</Title>
</SimilarProduct>

</SimilarProducts>
<info>
<appinfo>
<ASIN>info2</ASIN>
<Title>VA001</Title>
</appinfo>
<appinfo>
<ASIN>Descrizione</ASIN>

<Title>info3</Title>
</appinfo>
<appinfo>
<ASIN>Note</ASIN>
<Title />
</appinfo>
<appinfo>
<ASIN>Accessibilit&amp;agrave</ASIN>

<Title>non accessibile</Title>
</appinfo>
</info>
</Item>
</Items>
</ItemLookupResponse>


But I can't understand how convert XMLList in ArrayCollection for my dataGrid...

wvxvw
01-26-2009, 10:11 AM
You'll either need to use ADG / Tree, or write custom item renderers for your DG. DG is used to display flat data, i.e. only 1 nesting level, and your input has at least 3 levels.

See the example:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
applicationComplete="applicationCompleteHandler()">

<mx:Script>
<![CDATA[
private function applicationCompleteHandler():void
{
var serviceNamespace:Namespace = xml0.namespace();
list0 = xml0..serviceNamespace::Item;
}
]]>
</mx:Script>

<mx:XML id="xml0">
<ItemLookupResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://webservices.amazon.com/AWSECommerceService/2005-10-05">
<Items>
<Item>
<ItemAttributes>
<Title>infoLand</Title>
<ASIN>6</ASIN>
</ItemAttributes>
<SmallImage>
<URL>manager.png</URL>
</SmallImage>
<SimilarProducts>
<SimilarProduct>
<ASIN>3</ASIN>
<Title>Portale Comuni</Title>
</SimilarProduct>
</SimilarProducts>
<info>
<appinfo>
<ASIN>info2</ASIN>
<Title>VA001</Title>
</appinfo>
<appinfo>
<ASIN>Descrizione</ASIN>
<Title>info3</Title>
</appinfo>
<appinfo>
<ASIN>Note</ASIN>
<Title />
</appinfo>
<appinfo>
<ASIN>Accessibilit&amp;agrave</ASIN>
<Title>non accessibile</Title>
</appinfo>
</info>
</Item>
</Items>
</ItemLookupResponse>
</mx:XML>

<mx:XMLList id="list0" />

<mx:DataGrid dataProvider="{list0}" height="100%" width="100%">
<mx:columns>
<mx:DataGridColumn headerText="Product">
<mx:itemRenderer>
<mx:Component>
<mx:VBox height="80">
<mx:Script>
<![CDATA[
[Bindable]
private var _attribTitle:String;
[Bindable]
private var _attribASIN:String;
[Bindable]
private var _smallImageURL:String;
[Bindable]
private var _similarProducts:XMLList;

override public function set data(value:Object):void
{
var ns:Namespace = XML(value).namespace();
_attribTitle = XML(value)..ns::Title[0];
_attribASIN = XML(value)..ns::ASIN[0];
_smallImageURL = XML(value)..ns::URL[0];
_similarProducts = XML(value)..ns::SimilarProduct;
super.data = value;
}
]]>
</mx:Script>
<mx:Label text="{_attribTitle}" />
<mx:Label text="{_attribASIN}" />
<mx:Image source="{_smallImageURL}" />
</mx:VBox>
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
</mx:columns>
</mx:DataGrid>
</mx:Application>