Here you can find the source of dom2String2(Document document)
public static String dom2String2(Document document)
//package com.java2s; //License from project: Apache License import java.io.StringWriter; import org.w3c.dom.Document; import org.w3c.dom.bootstrap.DOMImplementationRegistry; import org.w3c.dom.ls.DOMImplementationLS; import org.w3c.dom.ls.LSOutput; import org.w3c.dom.ls.LSSerializer; public class Main { public static String dom2String2(Document document) { DOMImplementationRegistry registry; try {//from ww w.j a va2 s. co m registry = DOMImplementationRegistry.newInstance(); DOMImplementationLS domImplLS = (DOMImplementationLS) registry.getDOMImplementation("LS"); LSSerializer ser = domImplLS.createLSSerializer(); // Create a serializer for the DOM LSOutput out = domImplLS.createLSOutput(); StringWriter stringOut = new StringWriter(); // Writer will be a String out.setCharacterStream(stringOut); ser.write(document, out); // Serialize the DOM System.out.println("STRXML = " + stringOut.toString()); // Spit out the DOM as a String return stringOut.toString(); } catch (Exception e) { System.err.println("Cannot create registry: " + e.getMessage()); } return null; } }