Is it possible to get the child nodes under an xml element/node as a string in Java?
Or do you have iterate through everything?
Thanks,
Andez
Is it possible to get the child nodes under an xml element/node as a string in Java?
Or do you have iterate through everything?
Thanks,
Andez
Yep, like Andrzej said, it depends on the library, For example, jDOM has the useful XMLOutputter
class that can print to streams, or as a String, or whatever. Most powerful XML libraries will have similar functionality
You can use Transformer:
private String nodeToString(Node node) {
StringWriter sw = new StringWriter();
try {
Transformer t = TransformerFactory.newInstance().newTransformer();
t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
t.transform(new DOMSource(node), new StreamResult(sw));
} catch (TransformerException e) {
e.printStackTrace();
}
return sw.toString();
}