Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
Locked
0

Converting XMLList to ArrayCollection

New Here ,
Nov 13, 2008 Nov 13, 2008
Hi, I have an XML List as follows :
<items name="total">
<item name="value1" percentage="35"/>
<item name="value2" percentage="25"/>
<item name="value3" percentage="47"/>
<item name="value4" percentage="89"/>
</items>
<items name="somesub">
<item name="value1" percentage="15"/>
<item name="value2" percentage="27"/>
<item name="value3" percentage="56"/>
<item name="value4" percentage="43"/>
</items>

and I need to dynamically convert it to an array collection as follows (I'm loading the XML externally and the component only allows for an arrayCollection) :

myArray = new ArrayCollection(
[
{ name:"total", value1:35, value2:25, value3:47, value4:89},
{ name:"somesub", value1:15, value2:27, value3:56, value4:43}
]
)

I'm totally stuck on how to do this so any help putting me in the right direction would be highly appreciated.


22.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Nov 13, 2008 Nov 13, 2008
Two things come to mind:

You can set the result type to object and let HTTPService to the heavy lifting of converting XML to Objects for you:

http://blog.flexexamples.com/2007/09/19/converting-xml-to-objects-using-the-flex-httpservice-mxml-ta...

or you receive data as XML and use SimpleXMLDecoder class to convert it into objects and add them to a ArrayCollection

ATTA
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 13, 2008 Nov 13, 2008
Not too sure that this is the solution I need, I'm not familiar with the SimpleXMLDecoder function but it looks as if the XML data itself needs to be modelled already in order to be succesful.

I'm already a bit further on my quest to a solution, converting the content to an object does the trick indeed, at least when I hardcode it. I still am stuck on how to do this programatically / dynamic.

eg : the XMLdata is

<items name="total">
<item name="value1" percentage="35"/>
<item name="value2" percentage="25"/>
<item name="value3" percentage="47"/>
<item name="value4" percentage="89"/>
</items

and this needs to be dynamically converted to an object as follows :

var obj:Object = {name:"total", value1:35, value2:25, value3:47, value4:89}

Once I have this object the rest is simple, but I fail to do this conversion properly...


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Nov 26, 2009 Nov 26, 2009

its been a year since there was any activity on this thread so, is the question answered ?

if not... you can you an intermediate array and push elements one by one inside it and the convert this array into arraycollection.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 20, 2010 Jul 20, 2010

This works partiallly but it keeps all the XML Node names??  AND WHEN USED WITH A DATAGRID SEARCH iT BLOW UP IN FLEX

so help would still be nice

myData = new ArrayCollection(mx.utils.ArrayUtil.toArray(xmlStudents));

I found it on:

http://www.flexdeveloper.eu/forums/actionscript-3-0/converting-xml-to-arraycollection/

I ultimately needed it for this code examples:

HOW TO SEARCH A DATAGRID?

http://www.flex-blog.com/arraycollection-filter-example/

Thanks.

Doug Lubey of Louisiana

www.douglubey.com

SEARCH ENGINE REFERENCE:

flex4 datagrid search filter 2010

flex4 datagrid search filter columns

flex4 datagrid example with search filters

adobe flex4 convert fx:XMLList to arraylist

flex4 convert XMLList to arraylist

flashbuilder4 convert xml to arraylist

flex xml to arraylist

convert xmllist to arraycollection in flex

Implicit coercion of a value of type XMLList to an unrelated type mx.collections:ArrayList

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 20, 2010 Jul 20, 2010

Hi,

Here is the code which converts XMLList to an arraycollection...

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init();" layout="absolute">
<mx:Script>
  <![CDATA[
   import mx.collections.ArrayCollection;
   import mx.rpc.xml.SimpleXMLDecoder;
  
   private var acAllNodes:ArrayCollection;
   private var acOpenNodes:ArrayCollection;
  
   private function init():void
   {
    convertXmlListToArrayCollection();
   }
   private function convertXmlListToArrayCollection():void
   {
    var strXML:String = dpNodes.toXMLString();
    strXML = "<root>" + strXML + "</root>";
    var xml:XML = new XML(strXML);
    var xmlDoc:XMLDocument = new XMLDocument(xml);
    var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
    var resultObj:Object = decoder.decodeXML(xmlDoc);
    acAllNodes = new ArrayCollection();
   
    if(resultObj.root.hasOwnProperty("items"))
    {
     if(resultObj.root.items is ArrayCollection)
     {
      acAllNodes = resultObj.root.items;
     }
     else if(resultObj.root.items is Object)
     {
      acAllNodes.addItem(resultObj.root.items);
     }
    }   
   }
  ]]>
</mx:Script>
 
<mx:XMLListCollection id="dpNodes">
        <mx:source>
            <mx:XMLList>
                <items name="total">
    <item name="value1" percentage="35"/>
    <item name="value2" percentage="25"/>
    <item name="value3" percentage="47"/>
    <item name="value4" percentage="89"/>
    </items>
    <items name="somesub">
    <item name="value1" percentage="15"/>
    <item name="value2" percentage="27"/>
    <item name="value3" percentage="56"/>
    <item name="value4" percentage="43"/>
    </items>
            </mx:XMLList>
        </mx:source>
    </mx:XMLListCollection>

</mx:Application>

If this post answers your question or helps, please kindly mark it as such.


Thanks,

Bhasker Chari

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 21, 2010 Jul 21, 2010

Bhasker,

YES your solution worked...thanks.

     It does appear to be customized for each Xml document which uses different node names.  Took me 5 minutes to figure out your XML Set started with "items" where as mine started with "student"...thanks.

other than it worked perfectly:

var strXML:String = xmlStudents.toXMLString();

strXML =

"<root>" + strXML + "</root>";

//Alert.show(strXML, 'Alert Box', mx.controls.Alert.OK);

var xml:XML = new XML(strXML);

var xmlDoc:XMLDocument = new XMLDocument(xml);

var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);

var resultObj:Object = decoder.decodeXML(xmlDoc);

myData =

new ArrayCollection();

if(resultObj.root.hasOwnProperty("student"))

{

if(resultObj.root.student is ArrayCollection)

{

myData = resultObj.root.student;

}

else if(resultObj.root.student is Object)

{

myData.addItem(resultObj.root.student);

}

}

HERE IS MY fx:XMLLIST I based

<fx:XMLList id="xmlStudents">

     <student>

     <id>1</id>

     <name>Christina Coenraets</name>

     <phone>555-219-2270</phone>

     <email>ccoenraets@fictitious.com</email>

     <active>true</active>

     <image>images/arrow_icon_sm1.png</image>

     </student>

</fx:XMLList>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jul 21, 2010 Jul 21, 2010

Hi AICC,

I am glad it worked for you....Have a happy coding..

Thanks,

Bhasker

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jun 05, 2011 Jun 05, 2011

How i can do. if i want also childern from xml list.

if i am doing same way you show here and bind with tree its only give me the parent node its not give to me the childerns.

i stuck with my tree please let me know the solution.

thanks in advanced

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 08, 2012 Mar 08, 2012
LATEST
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines