4 Replies Latest reply on Jun 15, 2010 9:14 AM by srinth

    how to convert xml to HierarchicalData dynamic

    srinth Level 1

      Hi All,

       

      How to convert the below xml data to HierarchicalData (arrayCollection) dynamic.  

       

      <product model="Asus Eee PC T91" company="Asus" image="assets/Asus_Eee PC T91.png">
              <general>
                  <dimensions>10.3 * 6.5 * 1.05</dimensions>
                  <weight>2.6lb / 1.19kg</weight>
                  <startingPrice>$449</startingPrice>
                  <battery>3-cell Li-ion (std), 6-cell option</battery>
              </general>
          </product>
          <product model="Asus Eee PC T101H" company="Asus" image="assets/Asus_Eee PC T101H.png">
              <general>
                  <dimensions>10.3 * 6.5 * 1.05</dimensions>
                  <weight>2.6lb / 1.19kg</weight>
                  <startingPrice>$449</startingPrice>
                  <battery>3-cell Li-ion (std), 6-cell option</battery>
              </general> 

      </product>  

       

       

      eg:

      private var dpHierarchy:ArrayCollection = new ArrayCollection([
              {FEATURE:"Genarel", children: [
                  {FEATURE:"Dimensions (in) w*d*h", obj1:"10.3 * 6.56 * 0.99", obj2:"10.3 * 6.5 * 1.05"},
                  {FEATURE:"Weight (lb/Kg)", obj1l:"2.13lb / 1.01kg" , obj2:"2.6lb / 1.19kg"},
                  {FEATURE:"Starting Price (US only)", obj1:"$399", obj2:"$449"},
                  {FEATURE:"Battery", obj1:"TBD, 3-cell 26 Whr (up to 3.5hrs), 6-cell 52 Whr (up to 7hrs)", obj2:"3-cell Li-ion (std), 6-cell option"},
            ]},
      ]);

       

      Thanks in advance

        • 1. Re: how to convert xml to HierarchicalData dynamic
          BhaskerChari Level 4

          Hi Srinath,

           

          You can make use of the SimpleXmlDecoder class in order to accomplish this task.

           

          Check the below code..It works perfectly for what you required...

           

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="loadData();" layout="absolute">
          <mx:Script>
            <![CDATA[
             import mx.collections.ArrayCollection;
             import mx.rpc.xml.SimpleXMLDecoder;
            
             private var loader:URLLoader = new URLLoader();
             [Bindable]public var dpHierarchy:ArrayCollection = new ArrayCollection();
            
             private function loadData():void
             {   
              var request:URLRequest = new URLRequest();
              request.url = "data.xml";
              loader.load(request);
              loader.addEventListener(Event.COMPLETE,dataLoadComp);
             }
            
             private function dataLoadComp(event:Event):void
             {
              var xml:XML = new XML(loader.data);
              var xmlDoc:XMLDocument = new XMLDocument(xml);
              var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
              var obj:Object = decoder.decodeXML(xmlDoc);
              dpHierarchy = obj.products.product as ArrayCollection;   
             }  
            ]]>
          </mx:Script>
           
          </mx:Application>

           

          Note: You xml must contain only one root tag.

           

          So you xml should look....in data.xml file as below:

           

          <?xml version="1.0"?>
          <products>
          <product model="Asus Eee PC T91" company="Asus" image="assets/Asus_Eee PC T91.png">
              <general>
                  <dimensions>10.3 * 6.5 * 1.05</dimensions>
                  <weight>2.6lb / 1.19kg</weight>
                  <startingPrice>$449</startingPrice>
                  <battery>3-cell Li-ion (std), 6-cell option</battery>
              </general>
          </product>
          <product model="Asus Eee PC T101H" company="Asus" image="assets/Asus_Eee PC T101H.png">
              <general>
                  <dimensions>10.3 * 6.5 * 1.05</dimensions>
                  <weight>2.6lb / 1.19kg</weight>
                  <startingPrice>$449</startingPrice>
                  <battery>3-cell Li-ion (std), 6-cell option</battery>
              </general>
          </product>  
          </products>

           

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


          Thanks,

          Bhasker Chari

          • 2. Re: how to convert xml to HierarchicalData dynamic
            srinth Level 1

            Hi Bhasker Chari,

             

            How to diplay this data in Advancedatagrid as grouded item. i tried with the code you share but unable to achive in grouped.

             

            eg: 

             

             

            FEATUREProduct 1
            Product 2
            General
                dimensions copycopy
                weight  copycopy
                startingPrice     copycopy
                batterycopycopy
            Display
                screenSize  copycopy
                screenResolutioncopycopy

             

            Thanks ,

            Srinath

            • 3. Re: how to convert xml to HierarchicalData dynamic
              BhaskerChari Level 4

              Hi Srinath,

               

              But your question was how to convert xml to HierarchicalData dynamic..right..

               

              For displaying data you can structure your data and add AdvancedDataGrid columns accordingly..

               

              Thanks,

              Bhasker Chari

              • 4. Re: how to convert xml to HierarchicalData dynamic
                srinth Level 1

                Hi Bhasker Chari,

                 

                 

                 

                 

                Actually i need to display grouped view by using HierarchicalData format in advancedatagrid. using the below xml data:


                <notebook>
                <product model="Lenovo IdeaPad U150" company="Lenovo" image="assets/Lenovo IdeaPad U1501.png">
                          <general>
                               <dimensions>0.53” onwards thickness</dimensions>
                               <weight>2.97lb / 1.35kg</weight>
                               <startingPrice>699</startingPrice>
                          </general>
                          <display>
                               <screenSize>11.6” display</screenSize>
                               <screenResolution>1366 * 768</screenResolution>
                          </display>
                          <performance>
                               <storageSSD>64GB SSD</storageSSD>
                               <OS>Windows 7</OS>
                               <RAM>8GB DDR3 (max)</RAM>
                               <ODD>External SuperMulti DVD</ODD>
                          </performance>
                </product>
                <product model="Lenovo IdeaPad U1501" company="Lenovo" image="assets/Lenovo IdeaPad U1501.png">
                          <general>
                               <dimensions>0.53” onwards thickness</dimensions>
                               <weight>2.97lb / 1.35kg</weight>
                               <startingPrice>699</startingPrice>
                          </general>
                          <display>
                               <screenSize>11.6” display</screenSize>
                               <screenResolution>1366 * 768</screenResolution>
                          </display>
                          <performance>
                               <storageSSD>64GB SSD</storageSSD>
                               <OS>Windows 7</OS>
                               <RAM>8GB DDR3 (max)</RAM>
                               <ODD>External SuperMulti DVD</ODD>
                          </performance>
                </product>
                </notebook>
                

                 

                Thanks

                Srinath