Java – Convert org.w3c.document.Document to java.lang.String


private
static String toString(Document doc) throws Exception {

DOMSource domSource = new DOMSource(doc);

Transformer transformer = TransformerFactory.newInstance().newTransformer();

StringWriter sw = new StringWriter();

StreamResult sr = new StreamResult(sw);


transformer.transform(domSource, sr);


return
sw.toString();

}