java xml document.getTextContent()保持为空 [英] java xml document.getTextContent() stays empty

查看:301
本文介绍了java xml document.getTextContent()保持为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JUnit测试中构建一个xml文档。

I'm trying to build an xml document in a JUnit test.

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 yield null 。)我不知道为什么会这样。实际问题是后续的XPath表达式抛出错误:无法使用此上下文计算表达式

Yet the document stays empty (i.e. the println yields null.) I don'thave a clue why that is. The actual problem is that a subsequent XPath expression throws the error: Unable to evaluate expression using this context.

推荐答案

文档上的 getTextContent 的返回值被定义为null-参见节点

The return value of getTextContent on Document is defined to null- See Node.

要检索文本内容,请在根元素上调用getTextNode

To retreive the text contents call getTextNode on the root element

这篇关于java xml document.getTextContent()保持为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆