在Android中解析soap响应(字符串)

时间:2015-03-05 19:55:11

标签: android soap ksoap2 android-ksoap2

我将HttpPost的Soap响应作为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>

您能否建议我解析此回复的最佳方法

1 个答案:

答案 0 :(得分:0)

使用Android解析器之一。最流行的是XmlPullParser,这里描述的是: http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html

有一个非常简单的例子告诉你一切:)

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

您也可以在Vogella上看到:http://www.vogella.com/tutorials/AndroidXML/article.html