Here you can find the source of writeXMLFile(Document document, Writer writer)
public static void writeXMLFile(Document document, Writer writer)
//package com.java2s; // %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt import java.io.Writer; import org.w3c.dom.DOMConfiguration; import org.w3c.dom.DOMImplementation; import org.w3c.dom.Document; import org.w3c.dom.ls.DOMImplementationLS; import org.w3c.dom.ls.LSOutput; import org.w3c.dom.ls.LSSerializer; public class Main { private static final String LS_FEATURE_KEY = "LS"; private static final String LS_FEATURE_VERSION = "3.0"; private static final String CORE_FEATURE_KEY = "Core"; private static final String CORE_FEATURE_VERSION = "2.0"; public static void writeXMLFile(Document document, Writer writer) { DOMImplementation implementation = document.getImplementation(); if (implementation.hasFeature(LS_FEATURE_KEY, LS_FEATURE_VERSION) && implementation.hasFeature(CORE_FEATURE_KEY, CORE_FEATURE_VERSION)) { DOMImplementationLS implementationLS = (DOMImplementationLS) implementation.getFeature(LS_FEATURE_KEY, LS_FEATURE_VERSION); LSSerializer serializer = implementationLS.createLSSerializer(); DOMConfiguration configuration = serializer.getDomConfig(); configuration.setParameter("well-formed", Boolean.TRUE); configuration.setParameter("comments", true); LSOutput output = implementationLS.createLSOutput(); output.setEncoding("UTF-8"); output.setCharacterStream(writer); serializer.write(document, output); }//from w w w .j ava 2 s. c om } }