Parsing XML with kasoap2-Android

I am having difficulty parsing this xml structure:

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

      

Maybe someone can give me some examples of how to parse it?

My request looks like:

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 returns:

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

      

Thank.

+3


source to share


1 answer


Parsing the XML file:

Step 1: Create java bean class like:

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

//define methods get, set for fields
}

      



Step 2: using KSOAP2 to implement what you think with:

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 this is helpful for you.

+3


source







All Articles