Java XML Document Save to File saveDomToFile(Document doc, String filePath)

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

Description

Save content of DOM to a file.

License

CDDL license

Parameter

Parameter Description
doc Document
filePath String

Exception

Parameter Description
Exception an exception

Declaration

public static void saveDomToFile(Document doc, String filePath) throws Exception 

Method Source Code


//package com.java2s;
/*// ww  w  .j a  va2  s.  c o m
Copyright (c) 2013 eBay, Inc.
This program is licensed under the terms of the eBay Common Development and
Distribution License (CDDL) Version 1.0 (the "License") and any subsequent  version 
thereof released by eBay.  The then-current version of the License can be found 
at http://www.opensource.org/licenses/cddl1.php and in the eBaySDKLicense file that 
is under the eBay SDK ../docs directory.
*/

import java.io.File;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;

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 {
    /**
     * Save content of DOM to a file.
     * @param doc Document
     * @param filePath String
     * @throws Exception
     */
    public static void saveDomToFile(Document doc, String filePath) throws Exception {
        // Write the DOM to file.
        Source source = new DOMSource(doc);

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

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

Related

  1. saveDocumentToFile(Document doc, String filename)
  2. saveDOM(Document doc, String filename)
  3. SaveDOM(Document document, String filename)
  4. SaveDom(Document dom, String filepath)
  5. saveDOM(final Document doc, final OutputStream output)
  6. saveHumanReadable(Document document, File file)
  7. saveImpl(Document document, StreamResult output, Properties outputProperties)
  8. saveReportToFile(Node node, String documentFileName)
  9. saveTemporaryDocument(Document document, String folderName)