Java tutorial
//package com.java2s; import java.io.*; import java.util.*; import javax.xml.transform.*; import javax.xml.transform.dom.*; import javax.xml.transform.stream.*; import org.w3c.dom.*; public class Main { public static String getXml(Document doc) { DOMSource doms = new DOMSource(doc); StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); String xml = null; try { TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); Properties properties = t.getOutputProperties(); properties.setProperty(OutputKeys.ENCODING, "GB2312"); properties.setProperty(OutputKeys.METHOD, "xml");//! properties.setProperty(OutputKeys.VERSION, "1.0"); properties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperties(properties); t.transform(doms, sr); String dtd = doc.getDoctype().getInternalSubset(); if ((null != dtd) && (dtd.length() > 0)) { dtd = "\n<!DOCTYPE Catalog [\n" + dtd + "]>\n"; } ; xml = "<?xml version=\"1.0\" encoding=\"GB2312\"?>" + dtd; xml += sw.toString(); } catch (TransformerConfigurationException tce) { //"Transformer Configuration Exception\n-----" } catch (TransformerException te) { //"Transformer Exception\n---------" } return xml; } }