Java XML Document to File writeDomToFile(String filename, Document document)

Here you can find the source of writeDomToFile(String filename, Document document)

Description

This method writes a DOM document to a file

License

Open Source License

Parameter

Parameter Description
filename a parameter
document a parameter

Declaration

public static void writeDomToFile(String filename, Document document) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
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;

public class Main {
    /**/*www  . j  a  va  2s  . co  m*/
     * This method writes a DOM document to a file
     * 
     * @param filename
     * @param document
     */
    public static void writeDomToFile(String filename, Document document) {
        try {
            // Prepare the DOM document for writing
            Source source = new DOMSource(document);

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

            // Write the DOM document to the file
            // Get Transformer
            Transformer xformer = TransformerFactory.newInstance().newTransformer();
            // Write to a file
            xformer.transform(source, result);
        } catch (TransformerConfigurationException e) {
            System.out.println("TransformerConfigurationException: " + e);
        } catch (TransformerException e) {
            System.out.println("TransformerException: " + e);
        }
    }
}

Related

  1. writeDOMDocumentToFile(Document doc, String filePath)
  2. writeDomToFile(Document doc, String filename)
  3. writeDomToFile(Document dom, File outFile)
  4. writeDomToFile(Document dom, File outFile)
  5. writeDomToFile(Document dom, File outFile)
  6. writeFile(String fileName, Document content)
  7. writeTo(Document document, File output)
  8. writeToFile(Document doc, String file)
  9. writeToFile(Document doc, String filePath)