使用org.w3c.dom.Document读取XML节点内容,现在读取XML中节点内容都没有问题的。但如果节点中有一些HTML代码,读取的时候就会为空。在这些HTML代码上加上<![CDATA[  ]]>标签,读取的时候是就正常了,有没有办法不加这个CDATA标签就可以读取的?//构建xml对象
private Document buildDocument(String xmlData) throws IOException,
ParserConfigurationException, SAXException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// 创建临时文件
File temp = File.createTempFile(RandomUtils.generateString(10), ".xml");
// 设置程序退出时删除这个文件
temp.deleteOnExit();
// 向临时文件写数据
BufferedWriter out = new BufferedWriter(new FileWriter(temp));
out.write(xmlData);
out.close();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(temp);
}//读取值的方法
public String getValueByTagName(String parentTagName, String childTagName) { NodeList nl = this.doc.getElementsByTagName(parentTagName);
if (nl.getLength() < 1)
return null;
// 获取父节点下的所有子节点
NodeList child = nl.item(0).getChildNodes();
if (child.getLength() < 1)
return null;
for (int i = 0; i < child.getLength(); i++) {
Node nd = child.item(i);
if ((nd.getNodeName().equals(childTagName))) {
if (nd.getFirstChild() == null)
return null;
String message = nd.getFirstChild().getNodeValue();
return message;
}
}
return null;
}