Java XML Document to File writeXmlFile(Document doc, File file, boolean indent, String encoding)

Here you can find the source of writeXmlFile(Document doc, File file, boolean indent, String encoding)

Description

write Xml File

License

Open Source License

Declaration

public static void writeXmlFile(Document doc, File file, boolean indent, String encoding)
            throws TransformerFactoryConfigurationError, TransformerException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 Robert "Unlogic" Olofsson (unlogic@unlogic.se).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v3
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl-3.0-standalone.html
 ******************************************************************************/

import org.w3c.dom.Document;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;

import java.util.Map.Entry;

public class Main {
    public static void writeXmlFile(Document doc, File file, boolean indent, String encoding)
            throws TransformerFactoryConfigurationError, TransformerException {
        // Prepare the DOM document for writing
        Source source = new DOMSource(doc);

        // Prepare the output file
        Result result = new StreamResult(file);

        Transformer xformer = TransformerFactory.newInstance().newTransformer();

        xformer.setOutputProperty(OutputKeys.ENCODING, encoding);

        if (indent) {
            xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        }//from   w  w  w  .j  av  a 2s  . c o m

        xformer.transform(source, result);

    }

    public static void writeXmlFile(Document doc, File file, Entry<String, String>... outputKeys)
            throws TransformerFactoryConfigurationError, TransformerException {
        // Prepare the DOM document for writing
        Source source = new DOMSource(doc);

        // Prepare the output file
        Result result = new StreamResult(file);

        // Write the DOM document to the file
        Transformer xformer = TransformerFactory.newInstance().newTransformer();

        if (outputKeys != null) {

            for (Entry<String, String> entry : outputKeys) {

                xformer.setOutputProperty(entry.getKey(), entry.getValue());
            }
        }

        xformer.transform(source, result);

    }

    public static void writeXmlFile(Document doc, String filename, boolean indent, String encoding)
            throws TransformerFactoryConfigurationError, TransformerException {

        // Prepare the output file
        File file = new File(filename);

        writeXmlFile(doc, file, indent, encoding);

    }
}

Related

  1. writeXML(String OUTPUT_XML_FILE, org.w3c.dom.Document xmlDoc)
  2. writeXMLDocument(Document doc, String filename)
  3. writeXMLDocumentToFile(Document doc, String outputFilename)
  4. writeXmlFile(Document doc, File file)
  5. writeXmlFile(Document doc, File file)
  6. writeXmlFile(Document doc, String filename)
  7. writeXmlFile(Document doc, String filename)
  8. writeXmlFile(Document doc, String filename)
  9. writeXMLFile(Document document, Writer writer)