2 Replies Latest reply on Jul 13, 2010 4:26 AM by rootsounds

    Creating sub XMLList Collection from XML data

    Sen Imagines

      Hi,

       

      I am using three TileList components - A, B & C

      I get the data for the entire set as a single XML data from a remote XML generating function thru a HTTPService component

       

      e.g: single XML data:

       

      <nodes>

           <node id="1" type="a"></node>

           <node id="2" type="b"></node>

           <node id="3" type="c"></node>

           <node id="4" type="b"></node>

           <node id="5" type="c"></node>

           <node id="6" type="a"></node>

           <node id="7" type="a"></node>

      </nodes>

       

      I would like to create (group based on the "type" XML attribute) 3 sub XML List collection / XMLs using the received XML (as above) to provide as Data Provider for the corresponding three TileList component

       

      like:

       

      1. XMLA as data provider for A-tileList

      <nodes>

           <node id="1" type="a"></node>

           <node id="6" type="a"></node>

           <node id="7" type="a"></node>   

      </nodes>

       

      2. XMLB as data provider for B-tileList

      <nodes>

           <node id="2" type="b"></node>

           <node id="4" type="b"></node>

      </nodes>

       

      3. XMLC as data provider for C-tileList

       

      <nodes>

           <node id="3" type="c"></node>

           <node id="5" type="c"></node>

      </nodes>

      How can I accomplish this?
      -   Sen

       

        • 1. Re: Creating sub XMLList Collection from XML data
          BhaskerChari Level 4

          Hi Prabhu,

           

          I have done this by converting your xml to ArrayCollection and then creating seperate dataproviders for eah of your 3 TileLists..based on the type parameter...Check the code below:

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();">
          <mx:Script>
            <![CDATA[
             import mx.collections.ArrayCollection;
             import mx.rpc.xml.SimpleXMLDecoder;
            
             [Bindable]private var dataProviderA:ArrayCollection = new ArrayCollection();
             [Bindable]private var dataProviderB:ArrayCollection = new ArrayCollection();
             [Bindable]private var dataProviderC:ArrayCollection = new ArrayCollection();
            
             private function init():void
             {
              var xml:XML = testXML;
              var xmlDoc:XMLDocument = new XMLDocument(xml);
              var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
              var xmlObj:Object = decoder.decodeXML(xmlDoc);
              var resultObj:Object = xmlObj.nodes;
              var dataProvider:ArrayCollection;
             
              if(resultObj.node is ArrayCollection)
              {
               dataProvider = resultObj.node as ArrayCollection;
              }
              else if(resultObj.node is Object)
              {
               dataProvider.addItem(resultObj.node);
              }
              createDataProviders(dataProvider);
              tileListA.dataProvider = dataProviderA;
              tileListB.dataProvider = dataProviderB;
              tileListC.dataProvider = dataProviderC;
             }
             private function createDataProviders(dataProvider:ArrayCollection):void
             {
              for each(var objNode:* in dataProvider)
              {
               if(objNode.type == "a")
               {
                dataProviderA.addItem(objNode);
               }
               else if(objNode.type == "b")
               {
                dataProviderB.addItem(objNode);
               }
               else if(objNode.type == "c")
               {
                dataProviderC.addItem(objNode);
               }
              }
             }
               
            ]]>
          </mx:Script>

          <mx:XML xmlns="" id="testXML">
            <nodes>
                <node id="1" type="a"></node>
                <node id="2" type="b"></node>
                <node id="3" type="c"></node>
                <node id="4" type="b"></node>
                <node id="5" type="c"></node>
                <node id="6" type="a"></node>
                <node id="7" type="a"></node>
            </nodes>
          </mx:XML>


          <mx:VBox verticalGap="10" horizontalCenter="0">
            <mx:VBox horizontalAlign="center">
             <mx:Label text="Type A TileList"/>
             <mx:TileList id="tileListA" labelField="id" />
            </mx:VBox>
            <mx:VBox horizontalAlign="center">
             <mx:Label text="Type B TileList"/>
             <mx:TileList id="tileListB" labelField="id" />
            </mx:VBox>
            <mx:VBox horizontalAlign="center">
             <mx:Label text="Type C TileList"/>
             <mx:TileList id="tileListC" labelField="id" />
            </mx:VBox>
          </mx:VBox>

          </mx:Application>

          Note: You can also do this without converting to ArrayCollection directly by looping through the main Xml and creating seperate xml for each tilelist. However I follow this approach converting to ArrayCollection so that it will be easy for iteration.


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


          Thanks,

          Bhasker Chari

          1 person found this helpful
          • 2. Re: Creating sub XMLList Collection from XML data
            rootsounds Level 4

            This can be done very concisely with the help of E4X which makes working with XML a breeze.

             

            Here is my example which binds the data provider to all nodes of type "a" and specifies that the label field of the control be set to the id attribute of the node:

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
            <mx:XML id="testXML">
              <nodes>
                  <node id="1" type="a"></node>
                  <node id="2" type="b"></node>
                  <node id="3" type="c"></node>
                  <node id="4" type="b"></node>
                  <node id="5" type="c"></node>
                  <node id="6" type="a"></node>
                  <node id="7" type="a"></node>
              </nodes>
            </mx:XML>

            <mx:List dataProvider="{testXML.node.(@type == 'a')}" labelField="@id" />
            </mx:Application>