Java XML Document Save to File saveDocument(final Document document, final File destination)

Here you can find the source of saveDocument(final Document document, final File destination)

Description

save Document

License

Open Source License

Declaration

public static void saveDocument(final Document document, final File destination)
            throws IOException, TransformerException 

Method Source Code

//package com.java2s;
/**/*  w w  w.j  a  v  a 2s.  c  o  m*/
 * Copyright (C) 2015 BonitaSoft S.A.
 * BonitaSoft, 32 rue Gustave Eiffel - 38000 Grenoble
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation
 * version 2.1 of the License.
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License along with this
 * program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
 * Floor, Boston, MA 02110-1301, USA.
 **/

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

public class Main {
    public static void saveDocument(final Document document, final File destination)
            throws IOException, TransformerException {
        if (document == null) {
            throw new IllegalArgumentException("Document should not be null.");
        }
        final Transformer tf = TransformerFactory.newInstance().newTransformer();
        tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        tf.setOutputProperty(OutputKeys.INDENT, "yes");
        final FileOutputStream fos = new FileOutputStream(destination);
        try {
            final StreamResult outputTarget = new StreamResult(fos);
            tf.transform(new DOMSource(document), outputTarget);
        } finally {
            fos.close();
        }
    }
}

Related

  1. saveDocument(Document doc, File file)
  2. saveDocument(Document doc, String fullFileName)
  3. saveDocument(Document doc, Writer w)
  4. saveDocument(Document document, File outputFile)
  5. saveDocument(final Document doc, String encoding, String docPath)
  6. saveDocument(final Document document, String filename)
  7. saveDocument(String filename, byte[] document)
  8. saveDocumentToFile(Document doc, File file)
  9. saveDocumentToFile(Document doc, String filename)