5 Replies Latest reply on Jun 9, 2010 5:24 PM by ltambs

    parsing xml whose siblings are same names

    ltambs

      the xml I have to parse looks like below.

      <?xml version="1.0"?>
      <rows pos="309" totalRowCount="3101">
          <row id="1">
              <col>10</col>
              <col>2010-06-06 11:08:25.0</col>
          </row>
          <row id="2">
              <col>2</col>
              <col>2010-06-06 11:08:25.0</col>
          </row>
          <row id="3">
              <col>3</col>
              <col>2010-06-06 11:08:25.0</col>
          </row>
          <row id="4">
              <col>4</col>
              <col>2010-06-06 11:08:25.0</col>
          </row>
      </rows>

       

      in my datagrid

      <mx:columns>
        <mx:DataGridColumn headerText="Column 1" dataField="no[0]"/>
        <mx:DataGridColumn headerText="Column 2" dataField="no[1]"/>
      </mx:columns>

       

      how to bind the xml elements(col) to the datafield?

        • 1. Re: parsing xml whose siblings are same names
          BhaskerChari Level 4

          Hi,

           

          I dont think it would work out ...Its better to chnege the column names to col1 and col2.

           

          That way it woul be easy for you....

           

          So your new xml file should look like...

           

          <?xml version="1.0"?>

          <rows pos="309" totalRowCount="3101">

          <row id="1">

          <col1>10</col1>

          <col2>2010-06-06 11:08:25.0</col2>

          </row>

          <row id="2">

          <col1>2</col1>

          <col2>2010-06-06 11:08:25.0</col2>

          </row>

          <row id="3">

          <col1>3</col1>

          <col2>2010-06-06 11:08:25.0</col2>

          </row>

          <row id="4">

          <col1>4</col1>

          <col2>2010-06-06 11:08:25.0</col2>

          </row>

          </rows>

           

          and you can use the below application file...

           

           

          <?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 dpac: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);
              dpac = obj.rows.row as ArrayCollection;   
             }
            ]]>
          </mx:Script>

          <mx:DataGrid dataProvider="{dpac}">
            <mx:columns>
              <mx:DataGridColumn headerText="Column 1" dataField="col1"/>
              <mx:DataGridColumn headerText="Column 2" dataField="col2"/>
            </mx:columns>
          </mx:DataGrid>
          </mx:Application>

           

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


          Thanks,

          Bhasker Chari

          • 2. Re: parsing xml whose siblings are same names
            ltambs Level 1

            Thank.

            You are right.

            unfortunately I have to handle that kind of data with the structure.

            • 3. Re: parsing xml whose siblings are same names
              Matt Le Fevre Level 4

              If you have to keep them having the same node name, then i'd  recommend parsing the XML elsewhere into a different ArrayCollection or  something.

               

              read the XML in like it is (that'll allow  you to keep the node names as they are), then you'll need to run it  through a double loop to get out each column, here's a very messy example just to give you the idea of the concept.

               

              var i:int = 0;
              var j:int = 0;
              
              for each(var item:object in yourXML.rows)
              {
                   for each(var item2:object in item)
                   {
                        ArrCol[i]["col" + String(j)] = item2.toString();
                        j++;
                   }
                   i++;
                   j=0;
              }
              

               

              that'll give you a new Array Collection which you can distinguish between sibling nodes, use that as the dataprovider instead of the XML directly

              • 4. Re: parsing xml whose siblings are same names
                BhaskerChari Level 4

                Hi Itambs,

                 

                Here is the working code for your application with the same xml structure you wanted....

                 

                Sp in data.xml file put the xml structure you needed....

                 

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

                 

                <?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 dpac: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);
                    dpac = obj.rows.row as ArrayCollection;   
                   }  
                  ]]>
                </mx:Script>

                <mx:DataGrid dataProvider="{dpac}">
                  <mx:columns>
                    <mx:DataGridColumn headerText="Column 1">
                      <mx:itemRenderer>
                       <mx:Component>
                        <mx:Label text="{ArrayCollection(data.col).getItemAt(0)}">
                         <mx:Script>
                        <![CDATA[
                         import mx.collections.ArrayCollection;
                        ]]>
                       </mx:Script>
                        </mx:Label>
                       </mx:Component>
                      </mx:itemRenderer>
                    </mx:DataGridColumn>
                    <mx:DataGridColumn headerText="Column 2" dataField="col2">
                     <mx:itemRenderer>
                      <mx:Component>
                       <mx:Label text="{ArrayCollection(data.col).getItemAt(1)}">
                        <mx:Script>
                       <![CDATA[
                        import mx.collections.ArrayCollection;
                       ]]>
                      </mx:Script>
                       </mx:Label>
                      </mx:Component>
                     </mx:itemRenderer>
                    </mx:DataGridColumn>
                  </mx:columns>
                </mx:DataGrid>
                </mx:Application>

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


                Thanks,

                Bhasker Chari

                 

                • 5. Re: parsing xml whose siblings are same names
                  ltambs Level 1

                  Thanks a million.