6 Replies Latest reply on Mar 3, 2008 1:42 PM by krish283

    Datagrid, XML and HttpService

    krish283
      Hi,

      I am getting the following xml from a Http service. I am struggling to show it in a Datagrid. Please help.
      <results>
      <result>
      <row >
      <cell name="CLASS_GROUP">Cash and Equivalents</cell>
      <cell name="CLASS">Cash</cell>
      <cell name="STYLE">Cash</cell>
      </row>
      <row>
      <cell name="CLASS_GROUP">Cash and Equivalents</cell>
      <cell name="CLASS">Cash</cell>
      <cell name="STYLE">Sweep Fund</cell>
      </row>
      <row>
      <cell name="CLASS_GROUP">Equity</cell>
      <cell name="CLASS">Asia Equity</cell>
      <cell name="STYLE">Asia Pacific ex Japan Equity</cell>
      </row>
      </result>
      </results>

      The code to get the xml is:

      <mx:HTTPService
      id="photoService"
      url=" http://somehttpservice"
      resultFormat="e4x"
      result="photoResultHandler(event);"
      fault="photoFaultHandler(event);"
      />
      <mx:XMLListCollection id="myXC" source="{photoService.lastResult.result.row}"/>
        • 1. Re: Datagrid, XML and HttpService
          ericbelair Level 1
          This is a little difficult to debug without seeing the rest of your code. It would be good to see the function you are using top initiate the HTTPService send(), the HTTPService result handler, as well the DataGrid component code.
          • 2. Re: Datagrid, XML and HttpService
            krish283 Level 1
            Here is the full code.

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application
            xmlns:mx=" http://www.adobe.com/2006/mxml"
            xmlns:aral="*"
            creationComplete="photoService.send({_format: 'XML',_metadata: 'false'});"
            viewSourceURL="src/HTTPServiceConsumingFlickrRSSFeed/index.html"
            >
            <!-- Model -->


            <!-- Define the HTTP service -->
            <mx:HTTPService
            id="photoService"
            url=" http://someService"
            resultFormat="e4x"
            result="photoResultHandler(event);"
            fault="photoFaultHandler(event);"
            />


            <!-- Controller -->
            <mx:Script>
            <![CDATA[
            import flash.net.navigateToURL;
            import mx.controls.Alert;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.ResultEvent;
            import mx.utils.ArrayUtil;
            import mx.collections.*;

            private function photoResultHandler(event:ResultEvent):void
            {
            Alert.show(event.result.toString());
            }

            // Fault handler - displays the error.
            private function photoFaultHandler(event:FaultEvent):void
            {
            Alert.show(event.fault.message, "Could not load photo feed");
            }
            ]]>
            </mx:Script>


            <mx:Fade id="fadeIn" duration="3000" alphaFrom="0" alphaTo="1"/>
            <mx:Fade id="fadeOut" duration="3000" alphaFrom="1" alphaTo="0"/>
            <mx:XMLListCollection id="myXC" source="{photoService.lastResult.result.row}"/>

            <!-- User Interface -->
            <mx:Panel title="Public photos" width="100%" height="100%">

            <mx:List id="prodList" dataProvider="{myXC}" width="595" height="174"/>

            <mx:DataGrid dataProvider="{myXC}">
            </mx:DataGrid>

            </mx:Panel>

            </mx:Application>
            • 3. Re: Datagrid, XML and HttpService
              Gaurav J Adobe Employee
              You should also try to use results element.

              photoService.lastResult.results.result.row

              Thanks,
              Gaurav
              • 4. Re: Datagrid, XML and HttpService
                krish283 Level 1
                Gaurav,

                I am getting the proper zml mapped to XMLListCollection as root (results) get mapped to lastresult. The issue is
                <cell name="CLASS_GROUP">Cash and Equivalents</cell>
                <cell name="CLASS">Cash</cell>
                <cell name="STYLE">Cash</cell>

                since all the starting tags are cell. How do i display it in a Datagrid.

                Thanks
                Krishan
                • 5. Re: Datagrid, XML and HttpService
                  Gaurav J Adobe Employee
                  Sorry I misread your post.

                  Here is what you can do:

                  <mx:DataGrid dataProvider="{myXC}" >
                  <mx:columns>
                  <mx:DataGridColumn headerText="Group" labelFunction="displayClassGroup" />
                  <mx:DataGridColumn headerText="Class" labelFunction="displayClass" />
                  <mx:DataGridColumn headerText="Style" labelFunction="displayStyle" />
                  </mx:columns>
                  </mx:DataGrid>

                  and then inside the display function extract the value of the xml. like:

                  protected function displayClassGroup( row:Object, column:DataGridColumn ):String {
                  var xmlObj:XMLList = row.cell;
                  return xmlObj.(@name=="CLASS_GROUP");
                  }

                  Thanks,
                  Gaurav Jain
                  Flex SDK Team
                  • 6. Re: Datagrid, XML and HttpService
                    krish283 Level 1
                    Thanks Gaurav.
                    That solved the problem.