Java tutorial
//package com.java2s; import java.io.IOException; import java.io.StringWriter; import java.io.Writer; import javax.xml.transform.OutputKeys; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; public class Main { private static final TransformerFactory transFactory = TransformerFactory.newInstance(); public static String sourceToString(Source source) throws IOException { try { Transformer trans = transFactory.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); Writer writer = new StringWriter(); trans.transform(source, new StreamResult(writer)); writer.flush(); return writer.toString(); } catch (TransformerException ex) { throw new IOException(ex); } } }