我在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;
}
答案 0 :(得分:1)
从字符串
获取文档时添加此项 dbf.setCoalescing(true);
其中dbf是
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();