Here you can find the source of getXml(Document doc)
public static String getXml(Document doc) throws TransformerException
//package com.java2s; 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 { public static String getXml(Document doc) throws TransformerException { TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.METHOD, "xml"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); trans.setOutputProperty(//from w w w . j a va2s . c o m "{http://xml.apache.org/xslt}indent-amount", Integer.toString(2)); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(doc.getDocumentElement()); trans.transform(source, result); return sw.toString(); } }