1 Reply Latest reply: May 9, 2013 12:28 PM by Peter Farland RSS

    WebService response object in XML - parsing attributes

    mole2000

      Hi- new to Flex and I'm trying to parse out the attributes of the response object. I can get the entire object and see that its working, but I cant get just a single attribute. It pulls weather info for world cities. For example, I just want the location name and temperature.

       

      Any advice on this? Thanks!

       

      <?xml version="1.0"?>

      <!-- fds\rpc\RPCResultFaultMXML.mxml -->

      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"

                      xmlns:s="library://ns.adobe.com/flex/spark">

          <mx:Script>

              <![CDATA[

                  import mx.rpc.soap.SOAPFault;        

                  import mx.rpc.events.ResultEvent;

                  import mx.rpc.events.FaultEvent;

                  import mx.controls.Alert;

                  import mx.utils.ObjectUtil;

                 

                  public function showErrorDialog(event:FaultEvent):void {

                      // Handle operation fault.

                      Alert.show(event.fault.faultString, "Error");

                  }

                  public function defaultFault(event:FaultEvent):void {

                      // Handle service fault.

                      if (event.fault is SOAPFault) {

                          var fault:SOAPFault=event.fault as SOAPFault;

                          var faultElement:XML=fault.element;

                          // You could use E4X to traverse the raw fault element returned in the SOAP envelope.

                          // ...

                      }

                      Alert.show(event.fault.faultString, "Error");              

                  }

                  public function log(event:ResultEvent):void {

                      // Handle result.

                     

                      trace(event.result);

                      //trace(ObjectUtil.toString(event.result));

                      //var len:int;

                      //len = event.result.length;

                      //trace(len);

                      //trace(event.result);

                      //trace(event.result.GetWeatherResponse.Location);

                      //var myXML:XML = new XML(event.result);

                      //trace(myXML.attribute("Location"));

                     

                  }

              ]]>

          </mx:Script>

          <mx:WebService id="WeatherService" wsdl="http://www.webservicex.com/globalweather.asmx?wsdl"

                         fault="defaultFault(event)">

              <mx:operation name="GetWeather"

                            fault="showErrorDialog(event)"

                            result="log(event)"

                            resultFormat="xml">

                  <mx:request>

                      <CityName>{myCity.text}</CityName>

                      <CountryName>{myCountry.text}</CountryName>

                  </mx:request>

              </mx:operation>

          </mx:WebService>

          <mx:TextInput id="myCity" text="Madrid"/>

          <mx:TextInput id="myCountry" text="Spain"/>

          <!-- Call the web service operation with a Button click. -->

          <mx:Button width="60" label="Get Weather"

                    click="WeatherService.GetWeather.send();"/>

          <!-- Display the Weather -->

          <mx:Label text="Weather:"/>

          <mx:TextArea text="{WeatherService.GetWeather.lastResult}" height="200"/>

      </mx:Application>

        • 1. Re: WebService response object in XML - parsing attributes
          Peter Farland Adobe Employee

          The WSDL says GetWeatherResponse is a string, and it appears to be a string of XML, so set <mx:operation resultFormat="object"> as this will unwrap the SOAP response value and leave a string typed value intact. You can then create a new ActionScript 3.0 E4X-based XML instance from the unwrapped string:

           

              var myXML:XML = new XML(event.result.toString());

           

          You can then travese the XML document using E4X syntax, a simple example is included below:

           

              trace(myXML..Location);

              trace(myXML..Temperature);