public static InputStream documentToPrettyInputStream ( Node document ) throws Exception { ByteArrayOutputStream outputStream = new ByteArrayOutputStream ( ) ; Source xmlSource = new DOMSource ( document ) ; Result outputTarget = new StreamResult ( outputStream ) ; Transformer transformer = TransformerFactory . newInstance ( ) . newTransformer ( ) ; transformer . setOutputProperty ( OutputKeys . INDENT , "yes" ) ; transformer . setOutputProperty ( OutputKeys . METHOD , "xml" ) ; transformer . transform ( xmlSource , outputTarget ) ; InputStream is = new ByteArrayInputStream ( outputStream . toByteArray ( ) ) ; return is ; }
To output a DOM as a stream .