Java tutorial
//package com.java2s; import java.io.IOException; import java.io.StringWriter; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; public class Main { /** * ***************************************** * Convert XML document to string * ******************************************. * * @param xmlDocument the xml document * @return the string * @throws IOException Signals that an I/O exception has occurred. * @throws TransformerException the transformer exception */ public static String converXmlDocToString(Document xmlDocument) throws IOException, TransformerException { String xmlString = ""; TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(xmlDocument), new StreamResult(writer)); xmlString = writer.getBuffer().toString().replaceAll("\n|\r", ""); return xmlString; } }