得到“<”而不是“<”来自SOAP响应在一台设备上中断,而不是另一台设备

时间:2012-09-20 19:47:22

标签: android xml parsing dom ascii

我发送一个返回一些xml的SOAP POST。我一直在测试更新的设备(带有Android 4.1的Galaxy Nexus),它一直运行良好。但是,我刚尝试在较旧的设备(运行Android 2.2的HTC Desire HD)上运行它,我得到了ParseException: At line 1, column 0: unclosed token。以下是相关代码:

String xml = null;
Document doc = null;
String SOAPRequest = "[SOAP REQUEST HERE]";          

HttpPost httppost = new HttpPost("[WEBSITE HERE]");
InputStream soapResponse = null;
try {
    StringEntity postEntity = new StringEntity(SOAPRequest, HTTP.UTF_8);
    postEntity.setContentType("text/xml");
    httppost.setHeader("Content-Type", "application/soap+xml;charset=UTF-8");
    httppost.setEntity(postEntity);

    HttpClient httpclient = new DefaultHttpClient();
    BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient.execute(httppost);

    // Convert HttpResponse to InputStream
    HttpEntity responseEntity = httpResponse.getEntity();
    soapResponse = responseEntity.getContent();
    //// printing response here gives me ...<Result>&lt;blahblahblah&gt;<Result>...

    // Get the SearchResult xml
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = factory.newDocumentBuilder();
    doc = db.parse(soapResponse);

} catch ...

NodeList soapNodeList = doc.getElementsByTagName("Result");
xml = soapNodeList.item(0).getFirstChild().getNodeValue();

//// printing xml here gives me "<" 
return xml;

看一下httpResponse,我感兴趣的部分如下:<Result>&lt;blahblahblah&gt;</Result>

当我尝试使用xml获取此NodeList时,&lt;blahblahblah&gl;变成了<字符。

为什么这是一个问题,我该如何解决?

1 个答案:

答案 0 :(得分:1)

这可能是相关的:

android DOM parsing with entities in tags

...导致这个:

http://code.google.com/p/android/issues/detail?id=2607

...这似乎表明在早期的Android版本中,DOM解析器不能正确处理实体引用。那里的错误报告讨论了如何将实体视为一个单独的子节点,而不是合并到相邻的文本节点,这听起来很奇怪你的情况。

如果这是您遇到的问题,请尝试切换到使用SAX解析器。这是(恕我直言)这样一个更容易处理的XML解析器。