I'm trying to get the response from a soap webservice using Ksoap2 but all i get is a string with the xml file inside, anyone know how I can parse each property?
here is the code I'm using:
SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope soapEnvelope = new write(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);
transport = new HttpTransportSE(URL);
transport.debug=true;
transport.call(SOAP_ACTION, soapEnvelope);
response = (SoapObject) soapEnvelope.bodyIn;
And this is the Response I get:
Response{
Result= <raiz>
<result>
<exitoso>val</exitoso>
<message>message</message>
</result>
<clients>
<client>
<id>id</id>
<name>name</name>
<lastname>lastname</lastname>
</client>
</clients>
</raiz>;
}
Anyone know how can I get the actual data? like the name, ID and last name?
Ok, so it took me a bit too much and I didn't find an other way that actually worked for me so here is what i did.
first of all I call the web service which returns a CDATA with an xml document as a string like this:
<![CDATA[<users>
<user>
<username>myusername</username>
<password>myPassword</password>
</user>
</users>]]>
Once I get the response I created a class User:
public class user
{
public String userName, password;
}
And i have a class get_values
public class get_values {
public static Document doc;
//--- Returns an ArrayList with the values of the User using the User Class
public static ArrayList<user> UserParser(SoapObject response)
{
XmlPullParserFactory parserFactory;
try{
parserFactory = XmlPullParserFactory.newInstance();
XmlPullParser parser = parserFactory.newPullParser();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Source xmlSource = new DOMSource(GetDocFromString(response.getProperty(0).toString()));
Result outputTarget = new StreamResult(outputStream);
TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
InputStream is = new ByteArrayInputStream(outputStream.toByteArray());
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
parser.setInput(is, null);
return UserProcessParser(parser);
} catch (XmlPullParserException e) {
} catch (IOException e) {
} catch (TransformerConfigurationException e) {
} catch (TransformerException e) {
e.printStackTrace();
}
return null;
}
//--- Creates an xml document using the web service's response
private static Document GetDocFromString(String xmlStr)
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try
{
builder = factory.newDocumentBuilder();
doc = builder.parse( new InputSource( new StringReader( xmlStr ) ) );
return doc;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//--- Returns the xml values as an ArrayList
private static ArrayList<user> UserProcessParser(XmlPullParser parser) throws XmlPullParserException, IOException
{
ArrayList<user> user_data = new ArrayList<>();
int eventType = parser.getEventType();
user_data user_returned = null;
while (eventType != XmlPullParser.END_DOCUMENT){
String elementName = null;
switch (eventType){
case XmlPullParser.START_TAG:
elementName = parser.getName();
if ("user".equals(elementName))
{
user_returned = new user_data();
user_data.add(user_returned);
}
else if (user_returned != null)
{
if ("username".equals(elementName))
{
user_returned.username = parser.nextText();
}
else if ("password".equals(elementName))
{
user_returned.password = parser.nextText();
}
}
break;
}
eventType = parser.next();
}
return user;
}
}
then I just call the function to get the arraylist with all the values:
ArrayList<user> user_data = get_values.UserParser(MY_WEBSERVICE_RESPONSE);
And I get all the values I need, then in case of needing it for any other web service i Add another Parser function and another process parser function, then if it returns null you just have to check the response.
Well that's the solution I ended up with I hope it can help anyone else