I am sending a SOAP POST that returns some xml. I’ve been testing on a newer device (Galaxy Nexus with Android 4.1) and it’s been working fine. However, I just tried running it on an older device (HTC Desire HD running Android 2.2), and I am getting a ParseException: At line 1, column 0: unclosed token. Here is the relevant code:
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><blahblahblah><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;
Taking a look at the httpResponse, the part that I am interested in looks like this: <Result><blahblahblah></Result>.
When I try to get this xml using the NodeList, <blahblahblah≷ turns into just the character <.
Why is this a problem, and how do I fix it?
This could be relevant:
android DOM parsing with entities in tags
…which leads to this:
http://code.google.com/p/android/issues/detail?id=2607
…which seems to indicate that on earlier Android versions the DOM parser doesn’t deal with entity references properly. The bug report there discusses something about how entities are treated as a separate child rather than merged into the adjacent text node(s) which sounds oddly like your situation.
If this is the problem you’re having then try switching to using the SAX parser. It’s (IMHO) just such an easier XML parser to deal with as well.