1

我是 Android 新手(移植 iOS/Objective C 应用程序),我必须读取 WebService 返回的 XML(在 JSON 中有一些节点)文件

响应如下所示:

<SOAP-ENV:Envelope><SOAP-ENV:Body><ns1:TPLoginResponse><TPLoginResult>[{"content": "some json content...."]</TPLoginResult></ns1:TPLoginResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>

我怎样才能简单地获取 TPLoginResult 的内容?

我试过 :

            httpTransport.call(SOAP_ACTION, envelope);

            Object response = envelope.getResponse();

            SoapObject SoapResponse = (SoapObject)envelope.bodyIn;

            Log.e("Info",  "response : " + SoapResponse.toString() ); // content is successfully received here 

            String SResponse =  SoapResponse.toString();

            Log.e("Info",  "DocumentBuilderFactory" );

            // parse the XML as a W3C Document
            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
            builderFactory.setNamespaceAware(true);
            DocumentBuilder builder = builderFactory.newDocumentBuilder();

            Document document = builder.parse( new InputSource(new StringReader(SResponse)) );

             NodeList nList = document.getElementsByTagName("TPLoginResult");

             Log.e("info","nList length : " + nList.getLength());

但我需要一个长度为 0 的 nList ...

我也试过:

  NodeList nList = document.getElementsByTagName("ns1:TPLoginResponse");

但相同的 0 长度结果..

有什么建议么 ?

4

3 回答 3

1

我认为关键在这一行:

builderFactory.setNamespaceAware(true);

如果您评论该行,您将能够获得该元素

document.getElementsByTagName("TPLoginResponse");

否则,您可能需要使用:

document.getElementsByTagNameNS(namespaceURI,tag);

但我从未使用过最后一行,所以我无法帮助你。

请让我知道这对你有用,我很感兴趣。

于 2012-06-13T12:17:58.063 回答
1

KSOAP Android是一个非常有用的使用 SOAP Web 服务的工具。我在我所有的 Android 应用程序中都使用了这个工具。我可以说它运作良好。

您可以在他们的wiki和整个互联网上找到示例。

于 2012-06-13T12:20:38.833 回答
0

最后我发现使用 ksoap 方法更容易

String LoginResponse = SoapResponse.getProperty(0).toString();
于 2012-06-13T13:26:34.490 回答