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

    XML File To load in datagrid

      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">

      And here is the grid code below

      <mx:DataGrid id="myDataGrid" dataProvider="{grds.lastResult.books.book}">
      <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">
      click="navigateToURL(new URLRequest(data.amazonUrl), 'blank');"


        • 1. Re: XML File To load in datagrid
          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:
          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");
          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)"
          useProxy="false" resultFormat="e4x">

          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:DataGridColumn dataField="room_id" headerText="Room ID"/>
          <mx:DataGridColumn dataField="room_name" headerText="Room Name"/>
          <mx:DataGridColumn dataField="game_type" headerText="Game Type"/>

          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:
          <room_name>room 1</room_name>

          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.