Java tutorial
//package com.java2s; import java.io.StringWriter; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.ls.DOMImplementationLS; import org.w3c.dom.ls.LSOutput; import org.w3c.dom.ls.LSSerializer; public class Main { public static String elementToString(Element element) { Document document = element.getOwnerDocument(); DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation(); LSSerializer serializer = domImplLS.createLSSerializer(); // Client assumes/requires UTF-8 response. LSOutput lsOutput = domImplLS.createLSOutput(); lsOutput.setEncoding("UTF-8"); StringWriter stringWriter = new StringWriter(); lsOutput.setCharacterStream(stringWriter); serializer.write(element, lsOutput); return stringWriter.toString(); } }