5 Replies Latest reply on Nov 11, 2010 5:22 AM by KapilArora021983

    How to Convert xmlListCollection to ArrayCollection

    Raghuvasa Level 1

      Hi All,

       

       

        i have XmlListCollection like this

       

               <mx:XMLListCollection id="msgList" source="{usersXML.item.name}" />

       

      i need to convert this xmlListCollection to ArrayCollection.

       

      is there any chance to do that.

       

      Can any one help me.

       

      Thanks

      Raghu.

        • 1. Re: How to Convert xmlListCollection to ArrayCollection
          Flex harUI Adobe Employee

          Other than speed, there usually isn't a requirement to do such a conversion when working with Flex controls.  Flex doesn't have any easy way of doing this.  There may be some libraries out there, but it might be just as easy to write a loop and copy the XML data into value objects.

           

          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

          Blog: http://blogs.adobe.com/aharui

          • 2. Re: How to Convert xmlListCollection to ArrayCollection
            AICC Level 1

            Any word on this problem:

             

            Is this possible with FLEX 4?

             

             

            ERROR I GET ....or is similar

            Description Resource Path Location Type
            1067: Implicit coercion of a value of type XMLList to an unrelated type XML. Main.mxml /SafDatabase/src line 62 Flex Problem

             

            Description Resource Path Location Type
            1067: Implicit coercion of a value of type XMLList to an unrelated type mx.collections:ArrayList. Main.mxml /SafDatabase/src line 64 Flex Problem

             

            thanks in advance.

            Doug Lubey of Louisiana

            www.douglubey.com

            • 3. Re: How to Convert xmlListCollection to ArrayCollection
              BhaskerChari Level 4

              Hi Raghuvasa,

               

              Check the below code which converts XmlListCollection to an ArrayCollection by making use of SimpleXMLDecoder class..

               

              <?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("parentNode"))
                  {
                   if(resultObj.root.parentNode is ArrayCollection)
                   {
                    acAllNodes = resultObj.root.parentNode;
                   }
                   else if(resultObj.root.parentNode is Object)
                   {
                    acAllNodes.addItem(resultObj.root.parentNode);
                   }
                  }   
                 }
                ]]>
              </mx:Script>
               
              <mx:XMLListCollection id="dpNodes">
                      <mx:source>
                          <mx:XMLList>
                              <parentNode ID="2" Title="Hand Tools" NodeType="root" isBranch="true" isOpen="true">
                    <equipment ID="0" Title="[Add New Category]" NodeType="subcat" isBranch="false" ParentID="2" isOpen="true"/>
                    <equipment ID="0" Title="[Add New Item]" NodeType="subcat" isBranch="false" ParentID="2" isOpen="false"/>
                  </parentNode>
                  <parentNode ID="4" Title="Power Tools" NodeType="root" isBranch="true" isOpen="false">
                    <equipment ID="10" Title="Hydraulics" NodeType="subcat" isBranch="true" ParentID="4" isOpen="false">
                      <item ID="8" Title="Tool 1" NodeType="item" isBranch="false"/>
                      <item ID="9" Title="Tool 2" NodeType="item" isBranch="false"/>
                      <item ID="10" Title="Tool 3" NodeType="item" isBranch="false"/>
                      <item ID="11" Title="Tool 4" NodeType="item" isBranch="false"/>
                      <item ID="12" Title="Tool 5" NodeType="item" isBranch="false"/>
                      <equipment ID="0" Title="[Add New Category]" NodeType="subcat" isBranch="false" ParentID="10" isOpen="false"/>
                      <equipment ID="0" Title="[Add New Item]" NodeType="subcat" isBranch="false" ParentID="10" isOpen="false"/>
                    </equipment>
                    <equipment ID="11" Title="Electric, Cordless" NodeType="subcat" isBranch="true" ParentID="4" isOpen="false">
                      <item ID="28" Title="9v screwdriver" NodeType="item" isBranch="false"/>
                      <equipment ID="12" Title="12V" NodeType="subcat" isBranch="true" ParentID="11" isOpen="false">
                        <equipment ID="0" Title="[Add New Category]" NodeType="subcat" isBranch="false" ParentID="12" isOpen="false"/>
                        <equipment ID="0" Title="[Add New Item]" NodeType="subcat" isBranch="false" ParentID="12" isOpen="false"/>
                      </equipment>
                      <equipment ID="13" Title="18V" NodeType="subcat" isBranch="true" ParentID="11" isOpen="false">
                        <equipment ID="0" Title="[Add New Category]" NodeType="subcat" isBranch="false" ParentID="13" isOpen="false"/>
                        <equipment ID="0" Title="[Add New Item]" NodeType="subcat" isBranch="false" ParentID="13" isOpen="false"/>
                      </equipment>
                      <equipment ID="0" Title="[Add New Category]" NodeType="subcat" isBranch="false" ParentID="11" isOpen="false"/>
                      <equipment ID="0" Title="[Add New Item]" NodeType="subcat" isBranch="false" ParentID="11" isOpen="false"/>
                    </equipment>
                  </parentNode>
                          </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

              • 4. Re: How to Convert xmlListCollection to ArrayCollection
                artaxerxe123

                Very good answer BhaskerChari. It helped me also!