【问题标题】:how can i show just a value from a web service xml我如何只显示来自 Web 服务 xml 的值
【发布时间】:2016-02-09 17:39:32
【问题描述】:

我有以下代码,它从结果中获取了一个字符串,但我试图找到一种方法,只显示 xml 服务字符串中的一个值,但我不能,如何只显示该值?,在 te 代码中您只能看到网络服务的连接,我是否需要解析 xml 字符串?

--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;
}}

【问题讨论】:

    标签: web-services android-studio xml-parsing ksoap


    【解决方案1】:

    使用这条线

    result = (SoapObject) envelope.getResponse();
    

    而不是

    result = envelope.getResponse().toString();
    

    现在如果你想访问 XML 标签上的一些值,你应该使用 getProperty() 方法。请参阅文档here

    例如,你可以试试这个:

    result.getProperty(1).toString();
    

    【讨论】:

      猜你喜欢
      • 2023-03-16
      • 2012-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多