3

我在解析这个结构 xml 时遇到了困难:

<Publications>
<Publication>
<PublicationID>1</PublicationID>
<PublisherID>1</PublisherID>
<Date>2012-03-28 13:39:04</Date>
</Publication>
<Publication>
<PublicationID>2</PublicationID>
<PublisherID>1</PublisherID>
<Date>2012-01-23 10:00:03</Date>
</Publication>
</Publications> 

也许有人可以给我一些如何解析它的想法?

我的请求看起来:

String method_name = "GetPublications";

// creating new SoapObject
soap = GetSoapObject(method_name);

SoapSerializationEnvelope envelope = GetEnvelope(soap);

HttpTransportSE androidHttpTransport = new HttpTransportSE(REQUEST_URL);

androidHttpTransport.call(NAMESPACE + method_name, envelope);

soap = (SoapObject) envelope.getResponse();

kSoap2-Android 正在返回:

anyType{Publications=anyType{Publication=anyType{PublicationID=1; PublisherID=1; Date=2012-03-28 13:39:04; }; Publication=anyType{PublicationID=2; PublisherID=1; Date=2012-01-23 10:00:03; }; }; }

谢谢。

4

1 回答 1

3

解析xml文件,如:

Step1:创建类java bean,如:

public class Publication { 
Integer PublicationId;
Integer PublisherID;
Date    date; 

//define methods get, set for fields
}

Step2:使用 KSOAP2 来实现你的想法 Using :

soap = (SoapObject) envelope.bodyIn
soapResult = (SoapObject)soap.getProperty(0);
for(int i=0;i<soapResult.getPropertyCount();i++)
{
   SoapObject so = (SoapObject) soapResult.getProperty(i);
 //here, you can get data from xml using so.getProperty("PublicationID") 
 //or the other tag in xml file.
}

希望它对你有用。

于 2012-04-10T13:20:57.237 回答