在 android 中从 SOAP 解析具有另一个数组的复杂响应

Parsing complex response having an array inside another from SOAP in android

我正在尝试使用 Ksoap2 从 soap web 服务获取响应,但我得到的只是一个包含 xml 文件的字符串,有人知道我如何解析每个 属性 吗?

这是我使用的代码:

 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;

这是我得到的回复:

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

有人知道我怎样才能得到实际数据吗?喜欢姓名、ID 和姓氏?

你能试试这个吗:

SoapObject rootNode = response.getProperty("Result");
for (int i=0; i<= rootNode.getPropertyCount(); i++) {
   SoapObject so = rootNode.getProperty(i);
   String name = so.getProperty("name").toString();
   //print name to log/console
}

按照文档进行。您可能需要解决任何转换或依赖项。你得到的名字是什么?

好吧,我花了太多时间,但我没有找到其他真正适合我的方法,所以这就是我所做的。

首先,我将网络服务 returns 称为 CDATA,其中包含 xml 文档作为字符串,如下所示:

<![CDATA[<users>
     <user>
        <username>myusername</username>
        <password>myPassword</password>
     </user>
</users>]]>

收到回复后,我创建了一个 class 用户:

public class user
{
    public String userName, password;
}

我有一个 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;
        }

    }

然后我只调用函数来获取包含所有值的数组列表:

ArrayList<user> user_data = get_values.UserParser(MY_WEBSERVICE_RESPONSE);

然后我得到了我需要的所有值,然后在任何其他 Web 服务需要它的情况下我添加另一个解析器函数和另一个进程解析器函数,然后如果它 returns null 你只需要检查响应。

嗯,这就是我最终得到的解决方案,我希望它可以帮助其他人