我正在尝试在 JUnit 测试中构建 xml 文档。
doc=docBuilder.newDocument();
Element root = doc.createElement("Settings");
doc.appendChild(root);
Element label0 = doc.createElement("label_0");
root.appendChild(label0);
String s=doc.getTextContent();
System.out.println(s);
然而该文档仍然是空的(即println
yields null
.)我不知道为什么会这样。实际问题是后续的 XPath 表达式会引发错误:Unable to evaluate expression using this context
.
返回值getTextContent
on Document
被定义为 null- 请参阅Node http://docs.oracle.com/javase/6/docs/api/org/w3c/dom/Node.html#getTextContent%28%29.
要检索文本内容,请调用根元素上的 getText Node
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)