1 Reply Latest reply on Dec 3, 2008 11:49 PM by Krish.praveen

    Unable to extract Data from XML

    pranay.kondekar@gmail.com
      Hi,
      I am using Yahoo Local Search Service which returns the XML response. I want to retrieve data from the XML that I received. I stuck at this point and unable to retrieve the response in to datagrid.
      The response XML is as follows

      <ResultSet xsi:schemaLocation="urn:yahoo:lcl http://api.local.yahoo.com/LocalSearchService/V3/LocalSearchResponse.xsd" totalResultsAvailable="224" totalResultsReturned="1" firstResultPosition="1" xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:lcl">
      <ResultSetMapUrl> http://maps.yahoo.com/broadband/?q1=San+Jose%2C+CA+95112&amp;tt=starbucks&amp;tp=1</Result SetMapUrl>
      <Result id="21590724">
      <Title>Starbucks</Title>
      <Address>145 W Santa Clara St</Address>
      <City>San Jose</City>
      <State>CA</State>
      <Phone>(408) 288-8803</Phone>
      <Latitude>37.335236</Latitude>
      <Longitude>-121.892765</Longitude>
      <Rating>
      <AverageRating>4</AverageRating>
      <TotalRatings>3</TotalRatings>
      <TotalReviews>2</TotalReviews>
      <LastReviewDate>1198029377</LastReviewDate>
      <LastReviewIntro>My previous review had incorrect data. That was not true, I am sorry, I didn't know. As far as food and drink goes, Starbucks is pretty good.</LastReviewIntro>
      </Rating>
      <Distance>0.96</Distance>
      <Url> http://local.yahoo.com/info-21590724-starbucks-san-jose</Url>
      <ClickUrl> http://local.yahoo.com/info-21590724-starbucks-san-jose</ClickUrl>
      <MapUrl> http://maps.yahoo.com/maps_result?q1=145+W+Santa+Clara+St+San+Jose+CA&amp;gid1=21590724</M apUrl>
      <BusinessUrl> http://www.starbucks.com/</BusinessUrl>
      <BusinessClickUrl> http://www.starbucks.com/</BusinessClickUrl>
      <Categories>
      <Category id="96926219">Cafes</Category>
      <Category id="96926236">Restaurants</Category>
      <Category id="96926169">Coffee Houses</Category>
      </Categories>
      </Result>
      </ResultSet>

      The code that I am writing to get Title and Address is as follows.

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
      layout="vertical"
      backgroundAlpha="0" backgroundColor="#FFFFFF">

      <mx:HTTPService
      id="weatherService"
      url=" http://local.yahooapis.com/LocalSearchService/V3/localSearch"
      resultFormat="e4x"
      result="resultHandler(event);"/>

      <mx:Script>
      <![CDATA[
      import mx.collections.ArrayCollection;
      import mx.rpc.events.ResultEvent;

      [Bindable]
      private var myResult:XML ;
      private var temp:XMLNode;
      private var list:ArrayCollection;

      public function requestWeather():void {
      weatherService.cancel();
      var params:Object = new Object();
      params.appid = "YahooDemo";
      params.query = "starbucks";
      params.zip = "95112";
      params.results = "1"
      weatherService.send(params);
      }

      public function resultHandler(event:ResultEvent):void {
      myResult = XML( event.result);
      temp = myResult.Result;
      list = ArrayCollection(event.result);
      }
      ]]>
      </mx:Script>

      <mx:Form width="400">
      <mx:FormItem label="Get Local Data">
      <mx:Button label="Get Data" click="requestWeather();"/>
      </mx:FormItem>
      <mx:FormItem label="Address">
      <mx:Text text="{myResult.Result.Address}"/>
      </mx:FormItem>
      <mx:FormItem label="Title">
      <mx:Text text="{myResult.Result.Title}"/>
      </mx:FormItem>
      </mx:Form>

      <mx:DataGrid
      dataProvider="{myResult}"
      x="80" y="141" width="262" height="92" id="shippingOptionsList" editable="false" enabled="true">
      <mx:columns>
      <mx:DataGridColumn headerText="Title" dataField="Result.Title" />
      <mx:DataGridColumn headerText="Address" dataField="Result.Address" />
      </mx:columns>
      </mx:DataGrid>
      <mx:Label text="Response as it is"/>
      <mx:TextArea id="resultFld" text="{myResult}" width="400" height="152"/>


      </mx:Application>


      Can somebody help me to find where I am going wrong?

      Thank you
      -Pranay

        • 1. Re: Unable to extract Data from XML
          Krish.praveen Level 1
          Hi..

          I am sending you the sample code for Google Search..
          Hope you can check..and u can go ahead

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" backgroundGradientColors="[0x000000,0x323232]"
          width="100%" height="100%" viewSourceURL="srcview/index.html" verticalAlign="middle" horizontalAlign="center">
          <mx:Script>
          <![CDATA[
          import com.adobe.webapis.events.*;
          import com.adobe.webapis.*;
          import com.adobe.serialization.json.*;

          import mx.controls.Alert;
          import mx.rpc.events.ResultEvent;
          import mx.rpc.http.HTTPService;

          [Bindable]
          private var dgResults:Array; // source for datagrid

          private function search():void
          {
          var service:HTTPService = new HTTPService();
          service.url = ' http://ajax.googleapis.com/ajax/services/search/web';
          service.request.v = '1.0';
          service.request.q = searchStr.text;
          service.resultFormat = 'text';
          service.addEventListener(ResultEvent.RESULT, onServerResponse);
          service.send();
          }
          private function onServerResponse(event:ResultEvent):void {
          try {
          var json:Object = JSON.decode(event.result as String);
          if (json.responseData.results is Array) {
          dg.dataProvider = json.responseData.results;
          }
          }
          catch (error:Error) {
          Alert.show("Error on search: " + error.message);
          }
          }
          private function goToURL():void
          {
          navigateToURL(new URLRequest(dg.selectedItem.url))
          }
          ]]>
          </mx:Script>
          <mx:HBox>
          <mx:Text text="Search Google:" color="#FFFFFF"/>
          <mx:TextInput id="searchStr" enter="search()" width="200"/>
          <mx:Button id="btnSearch" click="search()" label="Search"/>
          </mx:HBox>

          <mx:DataGrid id="dg" dataProvider="{dgResults}" width="90%" height="113" minColumnWidth="200"
          variableRowHeight="false" doubleClickEnabled="true" doubleClick="goToURL()" >
          <mx:columns>
          <mx:DataGridColumn dataField="url" headerText="URL" width="200"/>
          <mx:DataGridColumn dataField="titleNoFormatting" headerText="Title" wordWrap="true" width="300"/>
          </mx:columns>
          </mx:DataGrid>

          </mx:Application>