Java tutorial
//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.Element; public class Main { public static String getXMLString(Element elm, boolean htmlMode) throws TransformerException { /* TODO: Certain HTML entities, like "⇒", currently seem impossible to represent, as the HTML mode will output them in a named form unsupported by the Swing HTML parser (e.g. "⇐"). */ Transformer t; t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.METHOD, htmlMode ? "html" : "xml"); t.setOutputProperty(OutputKeys.INDENT, "no"); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, htmlMode ? "yes" : "no"); StringWriter ret = new StringWriter(); t.transform(new DOMSource(elm), new StreamResult(ret)); return ret.toString(); } }