1 Reply Latest reply on Nov 25, 2008 3:45 AM by Sam Rowe

    Retrieving data from XML containing namespaces?

    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. Does the namespaces in XML matter while retrieving data?
      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.Result}"
      x="80" y="141" width="262" height="92" id="shippingOptionsList" editable="false" enabled="true">
      <mx:columns>
      <mx:DataGridColumn headerText="Title" dataField="Title" />
      <mx:DataGridColumn headerText="Address" dataField="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: Retrieving data from XML containing namespaces?
          Sam Rowe
          Hi Pranay,

          It looks like your problem is indeed to do with the XML namespace being used.

          Try adding this line after the import statements:

          namespace yahoo = "urn:yahoo:lcl";

          (This is the namespace URI defined in the returned XML)

          The result handler should then look like this:

          public function resultHandler(event:ResultEvent):void {
          use namespace yahoo;
          myResult = XML( event.result);
          temp = myResult.Result;
          list = ArrayCollection(event.result);
          }