Java XML Document to File writeXmlFile(Document doc, String filename)

Here you can find the source of writeXmlFile(Document doc, String filename)

Description

Writes and org.w3c.dom.Document to a file

License

Open Source License

Parameter

Parameter Description
doc a parameter
filename a parameter

Exception

Parameter Description
TransformerFactoryConfigurationError an exception
TransformerException an exception

Declaration

public static void writeXmlFile(Document doc, String filename)
        throws TransformerFactoryConfigurationError, TransformerException 

Method Source Code


//package com.java2s;
/*//from  ww  w  .  j  a  v  a 2 s .c o  m
 * Author(s): Pascal Heus (pheus@opendatafoundation.org)
 * 
 * This product has been developed with the financial and
 * technical support of the UK Data Archive Data Exchange Tools
 * project (http://www.data-archive.ac.uk/dext/) and the
 * Open Data Foundation (http://www.opendatafoundation.org)
 * 
 * Copyright 2007 University of Essex (http://www.esds.ac.uk)
 * 
 * This program 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; either version 2.1 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 Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301  USA
 * The full text of the license is also available on the Internet at
 * http://www.gnu.org/copyleft/lesser.html
 * 
 */

import java.io.File;

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 org.w3c.dom.Document;

public class Main {
    /**
     * Writes and org.w3c.dom.Document to a file
     *
     * @param doc
     * @param filename
     * @throws TransformerFactoryConfigurationError
     * @throws TransformerException
     */
    public static void writeXmlFile(Document doc, String filename)
            throws TransformerFactoryConfigurationError, TransformerException {
        // Prepare the DOM document for writing
        Source source = new DOMSource(doc);

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

        // Write the DOM document to the file
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.transform(source, result);
    }
}

Related

  1. writeXMLDocumentToFile(Document doc, String outputFilename)
  2. writeXmlFile(Document doc, File file)
  3. writeXmlFile(Document doc, File file)
  4. writeXmlFile(Document doc, File file, boolean indent, String encoding)
  5. writeXmlFile(Document doc, String filename)
  6. writeXmlFile(Document doc, String filename)
  7. writeXMLFile(Document document, Writer writer)
  8. writeXmlFile(File file, Document document)
  9. writeXmlFile(String fileName, Document document)