Java tutorial
//package com.java2s; import java.io.*; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import org.w3c.dom.Element; public class Main { public static StringBuffer elementToString(final Element element) { return transfer(element, null); } public static StringBuffer transfer(Element element, StreamSource source) { try { StringWriter sw = new StringWriter(); Transformer trans = null; if (source != null) trans = TransformerFactory.newInstance().newTransformer(source); else trans = TransformerFactory.newInstance().newTransformer(); trans.transform(new DOMSource(element), new StreamResult(sw)); sw.close(); return sw.getBuffer(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } }