Search code examples
androidsoapksoap2android-ksoap2

Parse soap response(String) in Android


I have the Soap response from HttpPost as a String.

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

<soap:Body>
    <ns2:EnrollProfileResponse xmlns="http://www.myserver.com/ws/de" xmlns:ns2="http://www.myserver.com/ws/identityx" xmlns:ns3="http://www.myserver.com/de/metadata">

        <ResponseStatus>
            <ReturnCode>100</ReturnCode>
            <Message>SUCCESS</Message>
            <Description>Device Success</Description>
        </ResponseStatus>
    </ns2:EnrollProfileResponse>
</soap:Body>

</soap:Envelope>

Can you suggest me the best way to parse this response


Solution

  • Use one of Android parsers. Most popular is XmlPullParser wchich is described here: http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html

    there is very simple example that tells You everything :)

     public class SimpleXmlPullApp
     {
    
         public static void main (String args[])
             throws XmlPullParserException, IOException
         {
             XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
             factory.setNamespaceAware(true);
             XmlPullParser xpp = factory.newPullParser();
    
             xpp.setInput( new StringReader ( "<foo>Hello World!</foo>" ) );
             int eventType = xpp.getEventType();
             while (eventType != XmlPullParser.END_DOCUMENT) {
              if(eventType == XmlPullParser.START_DOCUMENT) {
                  System.out.println("Start document");
              } else if(eventType == XmlPullParser.START_TAG) {
                  System.out.println("Start tag "+xpp.getName());
              } else if(eventType == XmlPullParser.END_TAG) {
                  System.out.println("End tag "+xpp.getName());
              } else if(eventType == XmlPullParser.TEXT) {
                  System.out.println("Text "+xpp.getText());
              }
              eventType = xpp.next();
             }
             System.out.println("End document");
         }
     }
    

    You may also see on Vogella: http://www.vogella.com/tutorials/AndroidXML/article.html