Java tutorial
//package com.java2s; import java.io.ByteArrayOutputStream; import java.io.UnsupportedEncodingException; import java.util.Properties; 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 transferXmlToString(Document doc) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); Properties p = t.getOutputProperties(); p.setProperty(OutputKeys.ENCODING, "UTF-8"); t.setOutputProperties(p); t.transform(new DOMSource(doc), new StreamResult(bos)); } catch (NullPointerException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } String xmlStr = ""; try { xmlStr = bos.toString("UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return xmlStr; } }