public static String serialize ( Document document ) { StringWriter writer = new StringWriter ( ) ; try { TransformerFactory factory = TransformerFactory . newInstance ( ) ; Transformer transformer = factory . newTransformer ( ) ; DOMSource domSource = new DOMSource ( document ) ; StreamResult streamResult = new StreamResult ( writer ) ; transformer . transform ( domSource , streamResult ) ; } catch ( TransformerException e ) { throw new RuntimeException ( "Error when serializing XML document." , e ) ; } return writer . toString ( ) ; }
Serialize to XML String