WebService response object in XML - parsing attributes
hi- new flex , i'm trying parse out attributes of response object. can entire object , see working, cant single attribute. pulls weather info world cities. example, want location name , 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 soapfault) {
var fault:soapfault=event.fault soapfault;
var faultelement:xml=fault.element;
// use e4x traverse raw fault element returned in 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 web service operation button click. -->
<mx:button width="60" label="get weather"
click="weatherservice.getweather.send();"/>
<!-- display weather -->
<mx:label text="weather:"/>
<mx:textarea text="{weatherservice.getweather.lastresult}" height="200"/>
</mx:application>
the wsdl says getweatherresponse string, , appears string of xml, set <mx:operation resultformat="object"> unwrap soap response value , leave string typed value intact. can create new actionscript 3.0 e4x-based xml instance unwrapped string:
var myxml:xml = new xml(event.result.tostring());
you can travese xml document using e4x syntax, simple example included below:
trace(myxml..location);
trace(myxml..temperature);
More discussions in Flex (Read Only)
adobe
Comments
Post a Comment