1

XML を返す SOAP POST を送信しています。新しいデバイス (Galaxy Nexus with Android 4.1) でテストしてきましたが、問題なく動作しています。ただし、古いデバイス (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;変わります<

なぜこれが問題なのですか?どうすれば修正できますか?

4

1 に答える 1

1

これは関連する可能性があります:

タグ内のエンティティを使用したAndroid DOMの解析

...これは次のようになります。

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

...これは、以前の Android バージョンでは、DOM パーサーがエンティティ参照を適切に処理していないことを示しているようです。そこのバグレポートでは、エンティティが隣接するテキストノードにマージされるのではなく、別の子として扱われる方法について説明しています。これは、あなたの状況のように奇妙に聞こえます。

これが問題である場合は、SAX パーサーの使用に切り替えてみてください。それは(IMHO)扱いが非常に簡単なXMLパーサーでもあります。

于 2012-09-20T20:17:11.687 に答える