public String writeXmlDocumentAsString ( Document document ) throws IOException { try { Transformer tr = transformerFactory . newTransformer ( ) ; tr . setOutputProperty ( OutputKeys . ENCODING , "UTF-8" ) ; tr . setOutputProperty ( OutputKeys . INDENT , "yes" ) ; StringWriter writer = new StringWriter ( ) ; StreamResult result = new StreamResult ( writer ) ; TransformerFactory tf = TransformerFactory . newInstance ( ) ; tr . transform ( new DOMSource ( document ) , result ) ; return writer . toString ( ) ; } catch ( Exception e ) { throw new IOException ( e . getMessage ( ) , e ) ; } }
Serializes the Document to a String .