编程语言
首页 > 编程语言> > java-使用kasoap2-Android进行XML解析

java-使用kasoap2-Android进行XML解析

作者:互联网

我在解析此结构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; }; }; }

谢谢.

解决方法:

像这样解析xml文件:

步骤1:创建类java bean,如下所示:

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

//define methods get, set for fields
}

步骤2:使用KSOAP2通过使用来实现您的想法:

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

希望对您有用.

标签:android-ksoap2,java,android
来源: https://codeday.me/bug/20191201/2081237.html