将ksoap对象转换为xml时不完整的xml

时间:2014-12-03 19:02:59

标签: android xml ksoap

我想在Android应用中使用来自Web服务的XML输出的纬度经度信息。

http://www.webservicex.net/airport.asmx?op=getAirportInformationByAirportCode

在Web浏览器中检查输出时,我得到以下信息: 此XML文件似乎没有与之关联的任何样式信息。文档树如下所示。

<string xmlns="http://www.webserviceX.NET">
<NewDataSet> <Table> <AirportCode>MSP</AirportCode> <CityOrAirportName>MINNEAPOLIS INTL</CityOrAirportName> <Country>United States</Country> <CountryAbbrviation>US</CountryAbbrviation> <CountryCode>63</CountryCode> <GMTOffset>6</GMTOffset> <RunwayLengthFeet>10000</RunwayLengthFeet> <RunwayElevationFeet>841</RunwayElevationFeet> <LatitudeDegree>44</LatitudeDegree> <LatitudeMinute>54</LatitudeMinute> <LatitudeSecond>0</LatitudeSecond> <LatitudeNpeerS>N</LatitudeNpeerS> <LongitudeDegree>93</LongitudeDegree> <LongitudeMinute>13</LongitudeMinute> <LongitudeSeconds>0</LongitudeSeconds> <LongitudeEperW>W</LongitudeEperW> </Table> <Table> <AirportCode>MSP</AirportCode> <CityOrAirportName>MINNEAPOLIS INTL</CityOrAirportName> <Country>United States</Country> <CountryAbbrviation>US</CountryAbbrviation> <CountryCode>63</CountryCode> <GMTOffset>6</GMTOffset> <RunwayLengthFeet>10000</RunwayLengthFeet> <RunwayElevationFeet>841</RunwayElevationFeet> <LatitudeDegree>44</LatitudeDegree> <LatitudeMinute>54</LatitudeMinute> <LatitudeSecond>0</LatitudeSecond> <LatitudeNpeerS>N</LatitudeNpeerS> <LongitudeDegree>93</LongitudeDegree> <LongitudeMinute>13</LongitudeMinute> <LongitudeSeconds>0</LongitudeSeconds> <LongitudeEperW>W</LongitudeEperW> </Table> </NewDataSet>
</string>

我在以下代码中以SOAP对象的形式获取此内容:

private static String SOAP_ACTION = "http://www.webserviceX.NET/getAirportInformationByAirportCode";
private static String NAMESPACE = "http://www.webserviceX.NET";
private static String METHOD_NAME = "getAirportInformationByAirportCode";
private static String URL = "http://www.webservicex.NET/airport.asmx?WSDL";
public String source_textview, destination_textview;

//Initialize soap request + add parameters
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);       

//Use this to add parameters
request.addProperty("airportCode",airportCode);

//Declare the version of the SOAP request
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

envelope.setOutputSoapObject(request);
envelope.dotNet = true;

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

androidHttpTransport.call(SOAP_ACTION, envelope);

try {
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    androidHttpTransport.debug = true;
    //androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    //this is the actual part that will call the webservice
    androidHttpTransport.call(SOAP_ACTION, envelope);

    String result=androidHttpTransport.responseDump;
    Log.d("xml:", "is:" + result);      

} 
catch (Exception e) {
    e.printStackTrace();
}

我得到如下XML:

D/xml:(4736): is:<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><getAirportInformationByAirportCodeResponse xmlns="http://www.webserviceX.NET"><getAirportInformationByAirportCodeResult>&lt;NewDataSet /&gt;</getAirportInformationByAirportCodeResult></getAirportInformationByAirportCodeResponse></soap:Body></soap:Envelope>

1 个答案:

答案 0 :(得分:0)

您输入的代码没有编译,因此它对您包含的代码没有响应。除此之外,您向我们展示的响应是针对空参数机场代码的情况。我尝试清理你的代码(包含在下面)并且它有效。真的不知道你之前做错了什么。

private  String SOAP_ACTION = "http://www.webserviceX.NET/getAirportInformationByAirportCode";
private  String NAMESPACE = "http://www.webserviceX.NET";
private  String METHOD_NAME = "getAirportInformationByAirportCode";
private  String URL = "http://www.webservicex.NET/airport.asmx?WSDL";

@Override
protected String doInBackground(String... params) {

    //Initialize soap request + add parameters
    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);       

    //Use this to add parameters
    request.addProperty("airportCode","MSP");

    //Declare the version of the SOAP request
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

    envelope.setOutputSoapObject(request);
    envelope.dotNet = true;

    try {
        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

        androidHttpTransport.debug = true;
        //androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        //this is the actual part that will call the webservice
        androidHttpTransport.call(SOAP_ACTION, envelope);

        String result=androidHttpTransport.responseDump;

    } 
    catch (Exception e) {
        e.printStackTrace();
    }           
    return "aaa";
}

现在您必须将“getAirportInformationByAirportCodeResult”标记中的XML解析为文本。