-1

我正在连接到在线网络服务并且能够得到响应。我需要使用 ksoap2 解析这个响应。我尝试使用 SAX 解析器,我能够解析它。请帮助我了解如何使用 ksoap2 解析 xml

这是我的回复 xml

<string xmlns="http://www.webserviceX.NET/">
<StockQuotes>
<Stock>
<Symbol>M</Symbol>
<Last>39.86</Last>
<Date>11/26/2012</Date>
<Time>4:02pm</Time>
<Change>-1.87</Change>
<Open>41.00</Open>
<High>41.09</High>
<Low>39.65</Low>
<Volume>7968614</Volume>
<MktCap>15.765B</MktCap>
<PreviousClose>41.73</PreviousClose>
<PercentageChange>-4.48%</PercentageChange>
<AnnRange>30.38 - 42.17</AnnRange>
<Earns>3.227</Earns>
<P-E>12.93</P-E>
<Name>Macy's Inc Common</Name>
</Stock>
</StockQuotes>
</string>
4

1 回答 1

1

我向您粘贴了一个示例代码,用于解析来自使用 .NET Web 服务的 Ksoap2 的结果。我希望它有所帮助。

创建一个具有如下数据类型的类:

import org.ksoap2.serialization.KvmSerializable;
import org.ksoap2.serialization.PropertyInfo;

public class Provincias implements KvmSerializable {

    public int GCPRV;
    public String GDPRV;
    public int GFCTB;

    public Provincias(){}
    public Provincias(int gcprv, String gdprv, int gfctb)
    {
        this.GCPRV=gcprv;
        this.GDPRV=gdprv;
        this.GFCTB=gfctb;       
    }

    @Override
    public Object getProperty(int arg0) {
          switch(arg0)
            {
            case 0:
                return GCPRV;
            case 1:
                return GDPRV;
            case 2:
                return GFCTB;
            }

            return null;
    }

    @Override
    public int getPropertyCount() { 
        return 3;
    }

    @Override
    public void getPropertyInfo(int index, Hashtable arg1, PropertyInfo info) {
          switch(index)
            {
            case 0:
                info.type = PropertyInfo.INTEGER_CLASS;
                info.name = "GCPRV";
                break;
            case 1:
                info.type = PropertyInfo.STRING_CLASS;
                info.name = "GDPRV";
                break;
            case 2:
                info.type = PropertyInfo.INTEGER_CLASS;
                info.name = "GFCTB";
                break;
            default:break;
            }

    }

获得 SoapObject 响应后,您只需要查询它的属性并将它们绑定到您的类,如下所示:

   Provincias[] prov = new Provincias[ObjetoSoap.getPropertyCount()];
                for (int i = 0; i < prov.length; i++) {
                    SoapObject pii = (SoapObject)ObjetoSoap.getProperty(i);
                    Provincias provincia = new Provincias();      
                    provincia.GCPRV=Integer.parseInt(pii.getProperty(0).toString());
                    provincia.GDPRV =pii.getProperty(1).toString();
                    provincia.GFCTB=Integer.parseInt(pii.getProperty(2).toString());                
                    prov[i] = provincia;
           }

编辑:这里有方法描述:

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Cargar_Provincias_JavaResponse xmlns="http://tempuri.org/">
      <Cargar_Provincias_JavaResult>
        <Provincias_Class>
          <GCPRV>int</GCPRV>
          <GDPRV>string</GDPRV>
          <GFCTB>int</GFCTB>
        </Provincias_Class>
        <Provincias_Class>
          <GCPRV>int</GCPRV>
          <GDPRV>string</GDPRV>
          <GFCTB>int</GFCTB>
        </Provincias_Class>
      </Cargar_Provincias_JavaResult>
    </Cargar_Provincias_JavaResponse>
  </soap:Body>
</soap:Envelope>
于 2012-11-27T08:01:57.837 回答