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
.
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:
See also: How to pretty print XML from Java?
The return value of
getTextContent
onDocument
is defined to null- See Node.To retreive the text contents call getTextNode on the root element