views:

29

answers:

2

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

+1  A: 

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

http://www.jdom.org/docs/apidocs/index.html

Java Drinker
+1  A: 

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();
}
spektom