I have this content in XML:
<place>
<placeName>!@#$%&*?/_"'()-+;</placeName>
</place>
it’s correct when I view Page Source
<place>
<placeName>!@#$%&*?/_"'()-+;</placeName>
</place
I use org.w3c.dom.Document, org.w3c.dom.Element, … to get the content “placeName”. The problem is the DOM library remove the escaped special characters. It shows “!@#$%” in Android logcat. Why? How to fix it?
This is a part of my code, I use Node::getNodeValue to get values from the above XML:
public static Document getDocument(final String xml) {
Document doc = null;
final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
final DocumentBuilder db = dbf.newDocumentBuilder();
final InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (final ParserConfigurationException e) {
System.out.println("XML parse error: " + e.getMessage());
return null;
} catch (final SAXException e) {
System.out.println("Wrong XML file structure: " + e.getMessage());
return null;
} catch (final IOException e) {
System.out.println("I/O exeption: " + e.getMessage());
return null;
}
return doc;
}
private static String request() {
String line = null;
try {
final DefaultHttpClient httpClient = new DefaultHttpClient();
final HttpGet httpGet = new HttpGet("http://api-url.com");
final HttpResponse httpResponse = httpClient.execute(httpGet);
final HttpEntity httpEntity = httpResponse.getEntity();
line = EntityUtils.toString(httpEntity);
} catch (final UnsupportedEncodingException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
} catch (final MalformedURLException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
} catch (final IOException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
}
return line;
}
add this when you get document from string
where dbf is