Java tutorial
//package com.java2s; import org.w3c.dom.Node; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.io.StringWriter; public class Main { /** * a debug method * * @param node A node to be dumped to a string * @param omitDeclaration A boolean whether to omit the XML declaration * @return A string representation of the node. * @throws Exception If anything goes wrong. Error handling omitted. */ public static String dumpNode(Node node, boolean omitDeclaration) throws Exception { Transformer xformer = TransformerFactory.newInstance().newTransformer(); if (omitDeclaration) { xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } StringWriter sw = new StringWriter(); Result result = new StreamResult(sw); Source source = new DOMSource(node); xformer.transform(source, result); return sw.toString(); } }