Java XML Document Save to File save(Document doc, String file, String encoding)

Here you can find the source of save(Document doc, String file, String encoding)

Description

Save the XML document to a file.

License

Apache License

Parameter

Parameter Description
doc The XML document to save.
file The file to save the document to.
encoding The encoding to save the file as.

Exception

Parameter Description
TransformerException If there is an error while saving the XML.

Declaration

public static void save(Document doc, String file, String encoding) throws TransformerException 

Method Source Code

//package com.java2s;
/*****************************************************************************
 * //  w w  w .ja v  a 2 s. c  om
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.OutputStream;

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

import org.w3c.dom.Node;

public class Main {
    /**
     * Save the XML document to a file.
     * 
     * @param doc
     *            The XML document to save.
     * @param file
     *            The file to save the document to.
     * @param encoding
     *            The encoding to save the file as.
     * 
     * @throws TransformerException
     *             If there is an error while saving the XML.
     */
    public static void save(Document doc, String file, String encoding) throws TransformerException {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        // initialize StreamResult with File object to save to file

        Result result = new StreamResult(new File(file));
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
    }

    /**
     * Save the XML document to an output stream.
     * 
     * @param doc
     *            The XML document to save.
     * @param outStream
     *            The stream to save the document to.
     * @param encoding
     *            The encoding to save the file as.
     * 
     * @throws TransformerException
     *             If there is an error while saving the XML.
     */
    public static void save(Node doc, OutputStream outStream, String encoding) throws TransformerException {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

        // initialize StreamResult with File object to save to file
        Result result = new StreamResult(outStream);
        DOMSource source = new DOMSource(doc);
        transformer.transform(source, result);
    }
}

Related

  1. save(Document doc, File file)
  2. save(Document document, OutputStream out, Properties outputProperties)
  3. save(Document document, OutputStream outputStream)
  4. save(Document document, String fileName)
  5. save(Document document, String path)