3 Replies Latest reply on Jul 1, 2008 3:15 PM by realtime158

    XML File To load in datagrid

    realtime158
      Ok i am trying to load this simple xml file called books.xml into a grid using mx:HTTPService. Here is the xml code below what am i missing because the data doesn't show up in the grid?

      Also here is the service code

      <mx:HTTPService id='grds' url=' http://localhost/datagrid/books.xml' useProxy="false" method="POST">
      </mx:HTTPService>

      And here is the grid code below

      <mx:DataGrid id="myDataGrid" dataProvider="{grds.lastResult.books.book}">
      <mx:columns>
      <mx:DataGridColumn dataField="ISBN" headerText="ISBN" width="85"/>
      <mx:DataGridColumn dataField="title" headerText="Title"/>
      <mx:DataGridColumn dataField="author" headerText="Author"/>
      <mx:DataGridColumn dataField="amazonUrl" headerText="Web site">
      <mx:itemRenderer>
      <mx:Component>
      <mx:LinkButton
      label="Visit"
      click="navigateToURL(new URLRequest(data.amazonUrl), 'blank');"
      />
      </mx:Component>

      </mx:itemRenderer>
      </mx:DataGridColumn>
      </mx:columns>
      </mx:DataGrid>

        • 1. Re: XML File To load in datagrid
          JBeloncik
          I have an extra step in my code, which handles the result of the HTTPService call, then assigns it as the dataProvider for the dataGrid. One thing I found tricky though is the data result seems to already imply the root node of your XML. You might just try editing your code to be dataProvider="{grds.lastResult.book}"

          Here is my code if you want to give it a try:
          PHP code to generate XML:
          <?php
          include 'db.php';

          $player_id = $_POST['player_id'];

          echo "<?xml version='1.0' encoding='ISO-8859-1' ?>";
          echo "<data>";
          echo " <rooms>";
          $rs = query("SELECT `room_id`,`room_name`,`game_type_name` FROM
          `ROOMS` a, `GAME_TYPES` b WHERE a.game_type_id = b.game_type_id");
          if($rs)
          {
          while($row = mysql_fetch_array($rs))
          {
          $room_id = $row['room_id'];
          $room_name = $row['room_name'];
          $game_type_name = $row['game_type_name'];
          echo "<room>";
          echo "<room_id>$room_id</room_id>";
          echo "<room_name>$room_name</room_name>";
          echo "<game_type>$game_type_name</game_type>";
          echo "</room>";
          }
          }

          echo " </rooms>";
          echo "</data>";

          ?>

          HTTPService in my MXML:
          <mx:HTTPService id="getRoomsXML" url="get_rooms.php"
          method="POST" result="handleRoomResultXML(event)"
          fault="handleRoomFault(event)"
          useProxy="false" resultFormat="e4x">
          <mx:request>
          <player_id>{player_id}</player_id>
          </mx:request>
          </mx:HTTPService>

          Then, use the xml result as the dataProvider for my dataGrid:
          private function handleRoomResultXML(event:ResultEvent):void {
          roomsDataGrid.dataProvider = event.result.rooms.room;
          }

          Here is my MXML code for the dataGrid:
          <mx:DataGrid id="roomsDataGrid" width="90%">
          <mx:columns>
          <mx:DataGridColumn dataField="room_id" headerText="Room ID"/>
          <mx:DataGridColumn dataField="room_name" headerText="Room Name"/>
          <mx:DataGridColumn dataField="game_type" headerText="Game Type"/>
          </mx:columns>
          </mx:DataGrid>

          The trickiest part I found was in figuring out where in the XML tree I
          should start. Note that event.result == top level of your XML, so:
          <data>
          <rooms>
          <room>
          <room_id>1</room_id>
          <room_name>room 1</room_name>
          </room>
          </rooms>
          </data>

          is accessed as event.result.rooms.room

          Hope that helps!
          • 2. Re: XML File To load in datagrid
            Gregory Lafrance Level 6
            I think the only thing I changed was calling grds.send() in app creationComplete, can't remember, but check out this code and you'll see. You may want to have HTTPService resultFormat="e4x". Also, as Tracy often points out, better to handle results of HTTPService in result event handler instead of with lastResult.
            • 3. Re: XML File To load in datagrid
              realtime158 Level 1
              Yea you are right e4x worked with no problems.