Java XML Document to File writeDocumentTo(Document doc, File file)

Here you can find the source of writeDocumentTo(Document doc, File file)

Description

Writes the given Document to a File .

License

Open Source License

Parameter

Parameter Description
doc The Document to write.
file The File to write to.

Declaration

public static void writeDocumentTo(Document doc, File file) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;

import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;

public class Main {
    private static final TransformerFactory m_xformFactory = TransformerFactory.newInstance();

    /**//w w  w  .j av  a 2  s.  c om
     * Writes the given {@link Document} to a {@link File}.
     *
     * @param doc The {@link Document} to write.
     * @param file The {@link File} to write to.
     * @see #encodeDocument(Document,boolean)
     * @see #writeDocumentTo(Document,OutputStream)
     */
    public static void writeDocumentTo(Document doc, File file) throws IOException {
        final FileOutputStream fos = new FileOutputStream(file);
        try {
            writeDocumentTo(doc, fos);
        } finally {
            fos.close();
        }
    }

    /**
     * Writes the given {@link Document} to an {@link OutputStream} using
     * UTF-8 encoding.
     *
     * @param doc The {@link Document} to write.
     * @param out The {@link OutputStream} to write to.
     * @see #encodeDocument(Document,boolean)
     * @see #writeDocumentTo(Document,File)
     */
    public static void writeDocumentTo(Document doc, OutputStream out) throws IOException {
        final DOMSource source = new DOMSource(doc);
        final Writer writer = new OutputStreamWriter(out, "UTF-8");
        final StreamResult result = new StreamResult(writer);
        final Transformer xform = createTransformer();
        try {
            xform.transform(source, result);
        } catch (TransformerException e) {
            throw new IOException("Couldn't write XML: " + e.getMessage());
        }
    }

    /**
     * Create a {@link Transformer} just the way we like it.
     *
     * @return Returns a new {@link Transformer}.
     */
    private static Transformer createTransformer() {
        final Transformer xform;
        try {
            xform = m_xformFactory.newTransformer();
        } catch (TransformerConfigurationException e) {
            throw new IllegalStateException(e);
        }
        xform.setOutputProperty(OutputKeys.INDENT, "yes");
        xform.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2");
        xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        return xform;
    }
}

Related

  1. writeDocument(Document doc, String filePath)
  2. writeDocument(Document doc, String filePath)
  3. writeDocument(Document document, File file)
  4. writeDocument(Document document, File file)
  5. writeDocument(Document document, String path)
  6. writeDocumentToFile(Document document, String filePathname, String method, int indent)
  7. writeDOM2XMLFile(Document domDoc, String fileName)
  8. writeDOMDocumentToFile(Document doc, String filePath)
  9. writeDomToFile(Document doc, String filename)