Java tutorial
//package com.java2s; 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.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.*; import java.io.*; public class Main { public static String domToXMLString(Node doc) { return sourceToXMLString(new DOMSource(doc)); } public static String sourceToXMLString(Source result) { String xmlResult = null; try { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); //transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); StringWriter out = new StringWriter(); StreamResult streamResult = new StreamResult(out); transformer.transform(result, streamResult); xmlResult = streamResult.getWriter().toString(); } catch (TransformerException e) { e.printStackTrace(); } return xmlResult; } }