public static String convertDocToString ( Document doc ) throws TransformerException { //set up a transformer TransformerFactory transfac = TransformerFactory . newInstance ( ) ; Transformer trans = transfac . newTransformer ( ) ; trans . setOutputProperty ( OutputKeys . OMIT_XML_DECLARATION , YES ) ; trans . setOutputProperty ( OutputKeys . INDENT , YES ) ; //create string from xml tree StringWriter sw = new StringWriter ( ) ; StreamResult result = new StreamResult ( sw ) ; DOMSource source = new DOMSource ( doc ) ; trans . transform ( source , result ) ; return sw . toString ( ) ; }
Convert a DOM document to a string