java xml document.getTextContent() stays empty

二次信任 提交于 2019-12-05 09:17:34

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

To retreive the text contents call getTextNode on the root element

prote

I imagine you want to serialize the document to pass it to the test case. To do this you have to pass your document to an empty XSL transformer, like this:

Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
//initialize StreamResult with File object to save to file
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
String xmlString = result.getWriter().toString();
System.out.println(xmlString);

See also: How to pretty print XML from Java?

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!