Java XML Document to File writeDocument(Document document, File file)

Here you can find the source of writeDocument(Document document, File file)

Description

Writes a document to a file.

License

Apache License

Parameter

Parameter Description
document The document to save.
file The file to save the document to.

Exception

Parameter Description
IOException if an error occurs
TransformerConfigurationException if an error occurs
TransformerException if an error occurs

Declaration

public static void writeDocument(Document document, File file)
        throws TransformerConfigurationException, TransformerException, IOException 

Method Source Code


//package com.java2s;
/*/*from   w w  w . j  av a2  s .co m*/
 * Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

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

import java.io.Writer;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
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;
import org.w3c.dom.DocumentType;

public class Main {
    /** 
     * Writes a document to a file. A new file is created if it does not exist.
     *
     * @param document The document to save.
     * @param file The file to save the document to.
     * 
     * @throws IOException if an error occurs
     * @throws TransformerConfigurationException if an error occurs
     * @throws TransformerException if an error occurs
     */
    public static void writeDocument(Document document, File file)
            throws TransformerConfigurationException, TransformerException, IOException {
        file.getParentFile().mkdirs();
        file.createNewFile();

        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(file);
        getTransformer(document.getDoctype()).transform(source, result);
    }

    /** 
     * Writes a document to a writer.
     *
     * @param document The document to write.
     * @param writer The writer to write the document to.
     * 
     * @throws IOException if an error occurs
     * @throws TransformerConfigurationException if an error occurs
     * @throws TransformerException if an error occurs
     */
    public static void writeDocument(Document document, Writer writer)
            throws TransformerConfigurationException, TransformerException, IOException {
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(writer);
        getTransformer(document.getDoctype()).transform(source, result);
    }

    /**
     * Get the tranformer.
     * 
     * @param documentType the document type
     * @return a transformer
     * 
     * @throws TransformerConfigurationException if an error occurs
     */
    protected static Transformer getTransformer(DocumentType documentType)
            throws TransformerConfigurationException {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");

        if (documentType != null) {
            transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, documentType.getPublicId());
            transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, documentType.getSystemId());
        }

        return transformer;
    }
}

Related

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