Java XML Document Save to File saveDocument(final Document doc, String encoding, String docPath)

Here you can find the source of saveDocument(final Document doc, String encoding, String docPath)

Description

Saves a WC3 DOM by writing it to an XML document.

License

Open Source License

Parameter

Parameter Description
doc The WC3 DOM document object.
docPath The full path to the XML document.
encoding Encoding scheme to use for the XML document, e.g., "UTF-8."

Exception

Parameter Description
TransformerConfigurationException an exception
FileNotFoundException an exception
UnsupportedEncodingException an exception
TransformerException an exception
IOException an exception

Declaration

public static void saveDocument(final Document doc, String encoding, String docPath)
        throws TransformerConfigurationException, FileNotFoundException, UnsupportedEncodingException,
        TransformerException, IOException 

Method Source Code


//package com.java2s;
/*//  www. ja  va  2 s. c o  m
 * Autopsy Forensic Browser
 *
 * Copyright 2012-2014 Basis Technology Corp.
 * Contact: carrier <at> sleuthkit <dot> org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
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 {
    /**
     * Saves a WC3 DOM by writing it to an XML document.
     *
     * @param doc The WC3 DOM document object.
     * @param docPath The full path to the XML document.
     * @param encoding Encoding scheme to use for the XML document, e.g.,
     * "UTF-8."
     * @throws TransformerConfigurationException
     * @throws FileNotFoundException
     * @throws UnsupportedEncodingException
     * @throws TransformerException
     * @throws IOException
     */
    public static void saveDocument(final Document doc, String encoding, String docPath)
            throws TransformerConfigurationException, FileNotFoundException, UnsupportedEncodingException,
            TransformerException, IOException {
        TransformerFactory xf = TransformerFactory.newInstance();
        xf.setAttribute("indent-number", 1); //NON-NLS
        Transformer xformer = xf.newTransformer();
        xformer.setOutputProperty(OutputKeys.METHOD, "xml"); //NON-NLS
        xformer.setOutputProperty(OutputKeys.INDENT, "yes"); //NON-NLS
        xformer.setOutputProperty(OutputKeys.ENCODING, encoding);
        xformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); //NON-NLS
        xformer.setOutputProperty(OutputKeys.VERSION, "1.0");
        File file = new File(docPath);
        try (FileOutputStream stream = new FileOutputStream(file)) {
            Result out = new StreamResult(new OutputStreamWriter(stream, encoding));
            xformer.transform(new DOMSource(doc), out);
            stream.flush();
        }
    }
}

Related

  1. saveDocument(Document doc, File file)
  2. saveDocument(Document doc, File file)
  3. saveDocument(Document doc, String fullFileName)
  4. saveDocument(Document doc, Writer w)
  5. saveDocument(Document document, File outputFile)
  6. saveDocument(final Document document, final File destination)
  7. saveDocument(final Document document, String filename)
  8. saveDocument(String filename, byte[] document)
  9. saveDocumentToFile(Document doc, File file)