我有以下代码,它从结果中获取一个字符串,但我试图找到一种方式,只显示xml服务字符串中的值,但我不能,如何只显示该值?,在te代码中如果我需要解析xml字符串,你只能看到Web服务的conexion吗?
- XML
<string xmlns="http://www.webserviceX.NET">
<?xml version="1.0" encoding="utf-16"?>
<CurrentWeather>
<Location>Mexico City / Licenci, Mexico (MMMX) 19-26N 099-06W</Location>
<Time>Feb 09, 2016 - 11:44 AM EST / 2016.02.09 1644 UTC</Time>
<Wind> Calm:0</Wind>
<Visibility> 7 mile(s):0</Visibility>
<SkyConditions> mostly cloudy</SkyConditions>
<Temperature> 51 F (11 C)</Temperature>
<DewPoint> 30 F (-1 C)</DewPoint>
<RelativeHumidity> 43%</RelativeHumidity>
<Pressure> 30.47 in. Hg (1031 hPa)</Pressure>
<Status>Success</Status>
</CurrentWeather>
</string>
-
public class UploadData {
public String getWeather(String city,String country){
String resultado = null;
SoapObject callWS;
callWS = new SoapObject("http://www.webserviceX.NET","GetWeather");
callWS.addProperty("CityName", city);
callWS.addProperty("CountryName",country);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = callWS;
envelope.dotNet = true;
envelope.encodingStyle = SoapSerializationEnvelope.XSD;
HttpTransportSE androidHttpTranport = null;
try {
String conexion = "http://www.webservicex.com/globalweather.asmx?WSDL";
androidHttpTranport = new HttpTransportSE(conexion);
androidHttpTranport.call("http://www.webserviceX.NET/GetWeather",envelope);
result = envelope.getResponse().toString();
}catch (Exception e){
System.out.println(e.getMessage());
result=e.getMessage();
}
return result;
}}
答案 0 :(得分:0)
使用此行
result = (SoapObject) envelope.getResponse();
而不是
result = envelope.getResponse().toString();
现在,如果要访问XML标记上的某些值,则应使用getProperty()方法。请参阅文档here。
例如,你可以试试这个:
result.getProperty(1).toString();