问题描述
<person>
<firstname>
<lastname>
<salary>
</person>
这是我解析的XML。当我尝试打印人的子元素的节点名称,
我得到
This is the XML I am parsing. When I try printing the node names of child elements of person,I get
firstname
firstname
lastname
工资
如何消除#text生成?
How do I eliminate #text being generated?
更新 -
这是我的代码
Update -Here is my code
try {
NodeList nl = null;
int l, i = 0;
File fXmlFile = new File("file.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
dbFactory.setValidating(false);
dbFactory.setIgnoringElementContentWhitespace(true);
dbFactory.setNamespaceAware(true);
dbFactory.setIgnoringComments(true);
dbFactory.setCoalescing(true);
InputStream in;
in = new FileInputStream(fXmlFile);
Document doc = dBuilder.parse(in);
doc.getDocumentElement().normalize();
Node n = doc.getDocumentElement();
System.out.println(dbFactory.isIgnoringElementContentWhitespace());
System.out.println(n);
if (n != null && n.hasChildNodes()) {
nl = n.getChildNodes();
for (i = 0; i < nl.getLength(); i++) {
System.out.println(nl.item(i).getNodeName());
}
}
} catch (Exception e) {
e.printStackTrace();
}
推荐答案
只有当您使用
才起作用,然后只有当您解析的XML文件引用解析器可以使用的DTD计算出仅空白空间的文本节点实际上是可忽略的。如果您的文档没有DTD,则会在安全方面发生错误,并假设不会忽略任何文本节点,因此您必须编写自己的代码,以便在遍历子节点时忽略它们。 setValidating(true)
时,setIgnoringElementContentWhitespace
setIgnoringElementContentWhitespace
only works if you use setValidating(true)
, and then only if the XML file you are parsing references a DTD that the parser can use to work out which whitespace-only text nodes are actually ignorable. If your document doesn't have a DTD it errs on the safe side and assumes that no text nodes can be ignored, so you'll have to write your own code to ignore them as you traverse the child nodes.
这篇关于一个XML节点的getNodeName()操作返回#text的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!