Java tutorial
//package com.java2s; import java.io.StringWriter; 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.Node; public class Main { /** * Returns a string representation of {@code node}. This method should only * be used for debugging purposes, at it creates a new transformer factory * and transformer upon each call. * * @param n * the node * @return a string representation of {@code node} */ public static String toString(Node n) { try { Transformer t = TransformerFactory.newInstance().newTransformer(); StringWriter sw = new StringWriter(); t.transform(new DOMSource(n), new StreamResult(sw)); return sw.toString(); } catch (TransformerException e) { return null; } } }