Java XML Document to File writeXMLDocumentToFile(Document doc, String outputFilename)

Here you can find the source of writeXMLDocumentToFile(Document doc, String outputFilename)

Description

Writes the xml document to the specified output file.

License

Open Source License

Parameter

Parameter Description

Declaration

private static void writeXMLDocumentToFile(Document doc, String outputFilename) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * QBiC Offer Generator provides an infrastructure for creating offers using QBiC portal and
 * infrastructure. Copyright (C) 2018 Benjamin Sailer
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program. If
 * not, see http://www.gnu.org/licenses/.
 *******************************************************************************/

import org.w3c.dom.Document;

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

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

public class Main {
    /**//  w  w  w. ja va 2s  .  co m
     * Writes the xml document to the specified output file.
     * @param doc: xml document to write
     * @param outputFilename: filename the document should be saved to
     */
    private static void writeXMLDocumentToFile(Document doc, String outputFilename) {

        // setup the transformer factory and the transformer for writing the xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = null;
        try {
            transformer = transformerFactory.newTransformer();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        }
        DOMSource domSource = new DOMSource(doc);

        // we want indentation for the generated xml file with 4 spaces
        assert transformer != null;
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

        // create the output file
        File outputFile = new File(outputFilename);
        try {
            outputFile.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

        // write the xml document to the output file
        StreamResult result = new StreamResult(outputFile.getAbsolutePath());
        try {
            transformer.transform(domSource, result);
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }
}

Related

  1. writeXml(Document doc, File outputFile)
  2. writeXML(Document doc, String fileName)
  3. writeXML(final File file, final Document doc)
  4. writeXML(String OUTPUT_XML_FILE, org.w3c.dom.Document xmlDoc)
  5. writeXMLDocument(Document doc, String filename)
  6. writeXmlFile(Document doc, File file)
  7. writeXmlFile(Document doc, File file)
  8. writeXmlFile(Document doc, File file, boolean indent, String encoding)
  9. writeXmlFile(Document doc, String filename)