0

I'm using SOAP service to get ticket. I'm sending user and pass, and I'm getting xml in String. For this I'm using ksoap2.

@Override
protected String doInBackground(String... params) {

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
    request.addProperty(USER, params[0]);
    request.addProperty(PASS, params[1]);

    SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    soapEnvelope.bodyOut = request;
    soapEnvelope.setOutputSoapObject(request);

    HttpTransportSE HttpTransport = new HttpTransportSE(URL);
    try {
        HttpTransport.call(SOAP_ACTION, soapEnvelope);
        return soapEnvelope.getResponse().toString();
    } catch (IOException | XmlPullParserException e) {
        e.printStackTrace();
        return null;
    }
}

@Override
    protected void onPostExecute(String XML) {
        super.onPostExecute(XML);
        if (result != null) {
           // Here I need to get data from XML

        }
    }

My XML String looks like this:

<?xml version="1.0" encoding="UTF-8" ?>
<resp err="0">
<ticket>1234567989</ticket>
</resp>

So I need to get the error number, and the ticket number.

Rao
  • 20,781
  • 11
  • 57
  • 77
KiKo
  • 1,668
  • 7
  • 27
  • 56
  • http://stackoverflow.com/questions/8408504/how-to-parse-a-string-containing-xml-in-java-and-retrieve-the-value-of-the-root – Apurva Feb 24 '15 at 15:34

2 Answers2

2

Don't convert your response to String and return it, instead get properties you need with getProperty() method and convert them to String. Here's how i was doing it:

    String ticket;

    public void getSoap() {

    SoapObject request = new SoapObject(NAMESPACE, METHODNAME);
    SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.implicitTypes = false;
    envelope.setOutputSoapObject(request);
    HttpTransportSE httpTransportSe = new HttpTransportSE(URL);
    httpTransportSe.debug = true;
    SoapObject response = null;

    try {
        httpTransportSe.call(SOAPACTION, envelope);
        response = (SoapObject) envelope.getResponse();
        ticket = response.getProperty("ticket").toString();

    } catch (Exception e) {
        e.printStackTrace();
    }

}

Getting "err" element from attribute might require a bit more research but you can try this where you get ticket:

String err = response.getAttribute("err").toString;

Hope this helps and happy coding !

Mel
  • 1,730
  • 17
  • 33
  • ...and what is response...? SoapObject..? – KiKo Feb 24 '15 at 15:59
  • I get this error: java.lang.ClassCastException: java.lang.String cannot be cast to org.ksoap2.serialization.SoapObject At: response = (SoapObject) envelope.getResponse(); – KiKo Feb 24 '15 at 16:10
  • Can you try with bodyIn ? Like this: response = (SoapObject) envelope.bodyIn; – Mel Feb 24 '15 at 16:22
  • I tried that, but then I get error: illegal property: ticket. I print the response and it looks like this: apiLoginResponse{return=123456; } – KiKo Feb 24 '15 at 16:28
  • Alright then bodyIn solves error on conversation. Getting properties varies on responses. For illegal property error, can you try getProperty(0) instead of getProperty("ticket") ? – Mel Feb 24 '15 at 16:44
  • 1
    Parsing result varies up to response, you should checkout properties but i believe you got the idea. – Mel Feb 24 '15 at 23:21
1

Use JDom to parse XML:

String xmlString = //get xml string;  

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
DocumentBuilder builder;  
try  
{  
    builder = factory.newDocumentBuilder();  
    Document document = builder.parse( new InputSource( new StringReader( xmlString ) ) );  
} catch (Exception e) {  
    e.printStackTrace();  
}

and next get values via XPath from document http://www.w3schools.com/xpath/xpath_examples.asp

Piotr Zych
  • 483
  • 4
  • 19