我刚接触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长度结果..
有什么建议吗?
答案 0 :(得分:1)
我认为关键在于这一行:
builderFactory.setNamespaceAware(true);
如果您评论该行,则可以使用
获取该元素document.getElementsByTagName("TPLoginResponse");
否则,您可能需要使用:
document.getElementsByTagNameNS(namespaceURI,tag);
但我从未使用过最后一行,所以我无法帮助你。
请让我知道这对你有用,我很感兴趣。
答案 1 :(得分:1)
KSOAP Android是一个非常有用的工具来使用SOAP Web服务。我在所有Android应用程序中都使用此工具。我可以说它运作良好。
您可以在wiki和整个互联网上找到示例。
答案 2 :(得分:0)
最后我发现使用jsoup方法更容易
String LoginResponse = SoapResponse.getProperty(0).toString();