Java XML Document to File writeDomToFile(Document dom, File outFile)

Here you can find the source of writeDomToFile(Document dom, File outFile)

Description

write Dom To File

License

Apache License

Parameter

Parameter Description
dom a parameter
outFile a parameter

Declaration

public static void writeDomToFile(Document dom, File outFile) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.io.FileOutputStream;

import java.io.OutputStreamWriter;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

public class Main {
    /**/*  w w  w  . ja va  2  s.  c o m*/
     * Document builder factory
     */
    private static DocumentBuilderFactory factory = null;
    private static TransformerFactory transFactory = null;

    /**
     * @param dom
     * @param outFile
     */
    public static void writeDomToFile(Document dom, File outFile) {
        try {
            Transformer transformer = transFactory.newTransformer();
            DOMSource source = new DOMSource(dom);
            StreamResult result;

            result = new StreamResult(
                    new OutputStreamWriter(new FileOutputStream(outFile), System.getProperty("file.encoding")));
            transformer.transform(source, result);
        } catch (Exception e) {
            throw new RuntimeException(
                    "Could not write dom tree '" + dom.getBaseURI() + "' to file '" + outFile.getName() + "'!", e);
        }
    }

    public static Document transform(Document dom, Document xslt) {
        try {
            DOMSource xsltSource = new DOMSource(xslt);
            Transformer transformer = transFactory.newTransformer(xsltSource);
            DOMSource source = new DOMSource(dom);
            Document out = newDocument();
            DOMResult result = new DOMResult(out);
            transformer.transform(source, result);
            return out;
        } catch (Exception e) {
            throw new RuntimeException("Could not write dom tree '" + dom.getBaseURI() + "'!", e);
        }
    }

    /**
     * Creates a new Document using the default XML implementation
     * 
     * @return DOM
     */
    public static Document newDocument() {
        try {
            return factory.newDocumentBuilder().newDocument();
        } catch (ParserConfigurationException e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }
}

Related

  1. writeDocumentToFile(Document document, String filePathname, String method, int indent)
  2. writeDOM2XMLFile(Document domDoc, String fileName)
  3. writeDOMDocumentToFile(Document doc, String filePath)
  4. writeDomToFile(Document doc, String filename)
  5. writeDomToFile(Document dom, File outFile)
  6. writeDomToFile(Document dom, File outFile)
  7. writeDomToFile(String filename, Document document)
  8. writeFile(String fileName, Document content)
  9. writeTo(Document document, File output)