我在 XML 中有这个内容:
<place>
<placeName>!@#$%&*?/_"'()-+;</placeName>
</place>
当我查看页面源时它是正确的
<place>
<placeName>!@#$%&*?/_"'()-+;</placeName>
</place
我使用 org.w3c.dom.Document, org.w3c.dom.Element, ... 来获取内容“placeName”。问题是 DOM 库删除了转义的特殊字符。它在 Android logcat 中显示“!@#$%”。为什么?如何解决?
这是我的代码的一部分,我使用 Node::getNodeValue 从上面的 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;
}