问题
i am connecnting to a online webservice and am able to get the response. i need to parse this response using the ksoap2. i tried with SAX parser and i am able to parse it.please help me on how to parse the xml using ksoap2
this is my response 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>
回答1:
I paste you a sample code I am using to parse results from Ksoap2 consuming .NET webservices. I hope it help.
Create a class with the data types like this:
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;
}
}
Once you have your SoapObject response, you just need to query it's propertis and bind them to your class like this:
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;
}
EDIT: Here you have the method description:
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>
来源:https://stackoverflow.com/questions/13579594/parsing-soap-response-in-android-using-ksoap2