Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java xml document.getTextContent() stays empty

Tags:

java

xml

xmldom

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);

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.

like image 784
xtofl Avatar asked Nov 03 '22 06:11

xtofl


1 Answers

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

To retreive the text contents call getTextNode on the root element

like image 65
jontro Avatar answered Nov 14 '22 22:11

jontro