1 Reply Latest reply on Jun 5, 2009 1:10 PM by FlexSeeker

    WebService XML Parsing

    FlexSeeker

      Hi,

       

      I am trying to parse the XML returned from this webservice http://ws.cdyne.com/WeatherWS/Weather.asmx?wsdl . I need to get the information in the city tag. I am not sure what I am wrong here. I am not sure how to get the City from the XML returned from the webservice. If I do a returnedXML.*; it prints the whole xml returned from the service, but how do I retrieve just the city?

       

      Thanks for your help.

       

      Here is my code:

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
              layout="vertical"
              verticalAlign="middle"
              backgroundColor="white"
              initialize="onLoad()">

       

          <mx:Script>
              <![CDATA[
                  import mx.controls.Alert;
                  import mx.rpc.events.ResultEvent;
                  import mx.rpc.events.FaultEvent;
                  import mx.utils.ObjectUtil;

       

                  private var returnXML:XML;
                 
                  private function onLoad():void {
                      webService.GetCityForecastByZIP.send();
                  }

       

                  private function GetCityForecastByZIP_result(evt:ResultEvent):void {
                     returnXML = new XML(evt.result);
                     textArea.text =returnXML.*;
                  }

       

                  private function GetCityForecastByZIP_fault(event:FaultEvent):void {
                      Alert.show(event.fault.faultCode + ": " + event.fault.faultString, "Error");
                  }

       


              ]]>
          </mx:Script>

       

          <mx:WebService id="webService" wsdl="http://ws.cdyne.com/WeatherWS/Weather.asmx?wsdl">
              <mx:operation name="GetCityForecastByZIP" resultFormat="e4x" result="GetCityForecastByZIP_result(event);" fault="GetCityForecastByZIP_fault(event);">
                  <mx:request>
                      <ZIP>07310</ZIP>
                  </mx:request>
              </mx:operation>
             
          </mx:WebService>

       

          <mx:TextArea id="textArea"
                  editable="false"
                  width="100%"
                  height="100%" />

       

      </mx:Application>

       

       

      Here is what I get back from the webservice If I print a returnXML.*

       

      <GetCityForecastByZIPResult xmlns="http://ws.cdyne.com/WeatherWS/">
        <Success>true</Success>
        <ResponseText>City Found</ResponseText>
        <State>NJ</State>
        <City>Jersey City</City>
        <WeatherStationCity>Newark</WeatherStationCity>
        <ForecastResult>
          <Forecast>
            <Date>2009-06-05T00:00:00</Date>
            <WeatherID>5</WeatherID>
            <Desciption>Rain</Desciption>
            <Temperatures>
              <MorningLow/>
              <DaytimeHigh>62</DaytimeHigh>
            </Temperatures>
            <ProbabilityOfPrecipiation>
              <Nighttime/>
              <Daytime/>
            </ProbabilityOfPrecipiation>
          </Forecast>
          <Forecast>
            <Date>2009-06-06T00:00:00</Date>
            <WeatherID>4</WeatherID>
            <Desciption>Sunny</Desciption>
            <Temperatures>
              <MorningLow>55</MorningLow>
              <DaytimeHigh>81</DaytimeHigh>
            </Temperatures>
            <ProbabilityOfPrecipiation>
              <Nighttime/>
              <Daytime/>
            </ProbabilityOfPrecipiation>
          </Forecast>

      </ForecastResult>
      </GetCityForecastByZIPResult>

        • 1. Re: WebService XML Parsing
          FlexSeeker Level 1

          I changed resultformat from e4x to xml.

           

          Here is the code that is working:

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                  layout="vertical"
                  verticalAlign="middle"
                  backgroundColor="white"
                  initialize="onLoad()">

           

              <mx:Script>
                  <![CDATA[
                      import mx.controls.Alert;
                      import mx.rpc.events.ResultEvent;
                      import mx.rpc.events.FaultEvent;
                      import mx.utils.ObjectUtil;

           

                      private var returnXML:XML;
                      private namespace weather = "http://ws.cdyne.com/WeatherWS/";
                      use namespace weather;
            
                      private function onLoad():void {
                          webService.GetCityForecastByZIP.send();
                      }

           

                      private function GetCityForecastByZIP_result(evt:ResultEvent):void {
                         returnXML = new XML(evt.result);
                         textArea.text ="result is "+returnXML.GetCityForecastByZIPResult.City;
                      }

           

                      private function GetCityForecastByZIP_fault(event:FaultEvent):void {
                          Alert.show(event.fault.faultCode + ": " + event.fault.faultString, "Error");
                      }

           


                  ]]>
              </mx:Script>

           

              <mx:WebService id="webService" wsdl="http://ws.cdyne.com/WeatherWS/Weather.asmx?wsdl">
                  <mx:operation name="GetCityForecastByZIP" resultFormat="xml" result="GetCityForecastByZIP_result(event);" fault="GetCityForecastByZIP_fault(event);">
                      <mx:request>
                          <ZIP>07310</ZIP>
                      </mx:request>
                  </mx:operation>
                 
              </mx:WebService>

           

              <mx:TextArea id="textArea"
                      editable="false"
                      width="100%"
                      height="100%" />

           

          </mx:Application>

           

          Thanks