Here you can find the source of renderElement(Element theElem)
public static String renderElement(Element theElem)
//package com.java2s; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.dom.DOMSource; import java.io.Writer; import java.io.StringWriter; import org.w3c.dom.Element; public class Main { public static String renderElement(Element theElem) { try {// ww w . j ava 2s . co m Writer aOutput = new StringWriter(); Source aSource = new DOMSource(theElem); StreamResult aStreamResult = new StreamResult(aOutput); Transformer aTransformer = TransformerFactory.newInstance().newTransformer(); aTransformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); aTransformer.setOutputProperty(OutputKeys.INDENT, "yes"); aTransformer.setOutputProperty(OutputKeys.METHOD, "html"); aTransformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); aTransformer.setOutputProperty(OutputKeys.STANDALONE, "no"); aTransformer.transform(aSource, aStreamResult); aOutput.close(); // hack because the elements were getting escaped for some reason return aOutput.toString().replaceAll(" ", " "); } catch (Exception ex) { throw new RuntimeException(ex); } } }