public static String getDocumentAsXmlString ( Document doc ) throws TransformerConfigurationException , TransformerException { DOMSource domSource = new DOMSource ( doc ) ; TransformerFactory tf = TransformerFactory . newInstance ( ) ; try { tf . setAttribute ( "indent-number" , 4 ) ; } catch ( IllegalArgumentException e ) { // ignore } Transformer transformer = tf . newTransformer ( ) ; //transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"yes"); transformer . setOutputProperty ( OutputKeys . METHOD , "xml" ) ; transformer . setOutputProperty ( OutputKeys . ENCODING , "UTF-8" ) ; transformer . setOutputProperty ( "{http://xml.apache.org/xslt}indent-amount" , "4" ) ; transformer . setOutputProperty ( OutputKeys . INDENT , "yes" ) ; // java . io . StringWriter sw = new java . io . StringWriter ( ) ; StreamResult sr = new StreamResult ( sw ) ; transformer . transform ( domSource , sr ) ; return sw . toString ( ) ; }
Return the XMLDocument formatted as a String