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

Parse soap response(String) in Android

我有来自 HttpPost 的 Soap 响应作为一个字符串。

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

你能建议我解析这个回复的最佳方法吗

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