使用kasoap2,Android的XML解析Android、XML

2023-09-06 17:01:25 作者:瑾年

我有困难解析这种结构的xml:

i have difficulties parsing this structure 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> 

也许有人可以给我一些的IDE如何解析呢?

Maybe some one could give me some ides how to parse it ?

我的要求如下:

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正在返回的:

kSoap2-Android is returning:

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

感谢。

推荐答案

解析XML文件,如:

第一步:创建一流的java bean这样的:

Step1: Creating class java bean like:

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

//define methods get, set for fields
}

第二步:使用KSOAP2来实现你的想法通过使用:

Step2: using KSOAP2 to implement what you think by 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.
}

希望这将是对您有用。

Hope it will be useful for you.