public static String nodeToString(Node document) throws Exception {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        Source xmlSource = new DOMSource(document);
        Result outputTarget = new StreamResult(outputStream);
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(xmlSource, outputTarget);

        return outputStream.toString();
    }
public static String nodeToString ( Node document ) throws Exception { ByteArrayOutputStream outputStream = new ByteArrayOutputStream ( ) ; Source xmlSource = new DOMSource ( document ) ; Result outputTarget = new StreamResult ( outputStream ) ; Transformer transformer = TransformerFactory . newInstance ( ) . newTransformer ( ) ; transformer . transform ( xmlSource , outputTarget ) ; return outputStream . toString ( ) ; }
To output a Node as a String.