Java XML Document to File writeXMLFile(Document document, Writer writer)

Here you can find the source of writeXMLFile(Document document, Writer writer)

Description

write XML File

License

Open Source License

Declaration

public static void writeXMLFile(Document document, Writer writer) 

Method Source Code

//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
    }
}

Related

  1. writeXmlFile(Document doc, File file)
  2. writeXmlFile(Document doc, File file, boolean indent, String encoding)
  3. writeXmlFile(Document doc, String filename)
  4. writeXmlFile(Document doc, String filename)
  5. writeXmlFile(Document doc, String filename)
  6. writeXmlFile(File file, Document document)
  7. writeXmlFile(String fileName, Document document)
  8. WriteXMLFile2(Document doc, String strFilePath)
  9. writeXmlToStream(Document doc, OutputStream stream)