Java XML Document to File writeDOMDocumentToFile(Document doc, String filePath)

Here you can find the source of writeDOMDocumentToFile(Document doc, String filePath)

Description

Persists a DOM document to a file

License

Open Source License

Parameter

Parameter Description
doc The DOM document
filePath The persistence file path

Exception

Parameter Description
IOException Wrong file path
TransformerException Software configuration error

Declaration

public static void writeDOMDocumentToFile(Document doc, String filePath)
        throws IOException, TransformerException 

Method Source Code


//package com.java2s;
/*/*from  www  . j  a v a  2  s.c o m*/
 * Copyright 2016 Mentor Graphics Corporation. All Rights Reserved.
 * <p>
 * Recipients who obtain this code directly from Mentor Graphics use it solely
 * for internal purposes to serve as example Java web services.
 * This code may not be used in a commercial distribution. Recipients may
 * duplicate the code provided that all notices are fully reproduced with
 * and remain in the code. No part of this code may be modified, reproduced,
 * translated, used, distributed, disclosed or provided to third parties
 * without the prior written consent of Mentor Graphics, except as expressly
 * authorized above.
 * <p>
 * THE CODE IS MADE AVAILABLE "AS IS" WITHOUT WARRANTY OR SUPPORT OF ANY KIND.
 * MENTOR GRAPHICS OFFERS NO EXPRESS OR IMPLIED WARRANTIES AND SPECIFICALLY
 * DISCLAIMS ANY WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
 * OR WARRANTY OF NON-INFRINGEMENT. IN NO EVENT SHALL MENTOR GRAPHICS OR ITS
 * LICENSORS BE LIABLE FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING LOST PROFITS OR SAVINGS) WHETHER BASED ON CONTRACT, TORT
 * OR ANY OTHER LEGAL THEORY, EVEN IF MENTOR GRAPHICS OR ITS LICENSORS HAVE BEEN
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * <p>
 */

import org.w3c.dom.Document;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.stream.StreamResult;

import javax.xml.transform.dom.DOMSource;

import java.io.OutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    private static Transformer _xmlTransformer;

    /**
     * Persists a DOM document to a file
     * @param doc The DOM document
     * @param filePath The persistence file path
     * @throws IOException Wrong file path
     * @throws TransformerException Software configuration error
     */
    public static void writeDOMDocumentToFile(Document doc, String filePath)
            throws IOException, TransformerException {
        File outFile = new File(filePath);
        File outDir = new File(outFile.getParent());
        outDir.mkdir();

        FileOutputStream stream = null;
        try {
            stream = new FileOutputStream(outFile);
            writeDOMDocumentToStream(doc, stream);
        } finally {
            if (stream != null) {
                stream.close();
            }
        }
    }

    /**
     * Serialises a DOM document to a stream.
     * @param doc The DOM document
     * @param stream The serialisation stream
     * @throws TransformerException Software configuration error
     */
    public static void writeDOMDocumentToStream(Document doc, OutputStream stream) throws TransformerException {
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(stream);
        getXMLTransformer().transform(source, result);
    }

    private static Transformer getXMLTransformer() throws TransformerConfigurationException {
        if (_xmlTransformer == null) {
            _xmlTransformer = TransformerFactory.newInstance().newTransformer();
            _xmlTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
        }
        return _xmlTransformer;
    }
}

Related

  1. writeDocument(Document document, File file)
  2. writeDocument(Document document, String path)
  3. writeDocumentTo(Document doc, File file)
  4. writeDocumentToFile(Document document, String filePathname, String method, int indent)
  5. writeDOM2XMLFile(Document domDoc, String fileName)
  6. writeDomToFile(Document doc, String filename)
  7. writeDomToFile(Document dom, File outFile)
  8. writeDomToFile(Document dom, File outFile)
  9. writeDomToFile(Document dom, File outFile)