public static String docToString2 ( Document domDoc ) throws IOException { try { TransformerFactory transFact = TransformerFactory . newInstance ( ) ; Transformer trans = transFact . newTransformer ( ) ; trans . setOutputProperty ( OutputKeys . INDENT , "no" ) ; StringWriter sw = new StringWriter ( ) ; Result result = new StreamResult ( sw ) ; trans . transform ( new DOMSource ( domDoc ) , result ) ; return sw . toString ( ) ; } catch ( Exception ex ) { throw new IOException ( String . format ( "Error converting from doc to string %s" , ex . getMessage ( ) ) ) ; } }
Convert a DOM tree into a String using transform