Java tutorial
//package com.java2s; import java.io.StringWriter; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; 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 XMLDocumentToString(Document _doc) { TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans; String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; try { trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(_doc); trans.transform(source, result); xmlString += sw.toString(); } catch (TransformerConfigurationException e) { System.out.println("C:" + e.getClass() + "\nM:" + e.getMessage()); return null; } catch (TransformerException e) { System.out.println("C:" + e.getClass() + "\nM:" + e.getMessage()); return null; } return xmlString; } }