Java XML Document Save to File saveDocument(Document doc, Writer w)

Here you can find the source of saveDocument(Document doc, Writer w)

Description

save Document

License

Open Source License

Declaration

public static void saveDocument(Document doc, Writer w)
            throws TransformerFactoryConfigurationError, TransformerException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2014 United States Government as represented by the
 * Administrator of the National Aeronautics and Space Administration.
 * All Rights Reserved./* w w  w .j  a  v a2s .  com*/
 * 
 * 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.Writer;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

public class Main {
    public static void saveDocument(Document doc, File file)
            throws TransformerFactoryConfigurationError, TransformerException {
        saveDocument(doc, new StreamResult(file));
    }

    public static void saveDocument(Document doc, Writer w)
            throws TransformerFactoryConfigurationError, TransformerException {
        saveDocument(doc, new StreamResult(w));
    }

    public static void saveDocument(Document doc, Result result)
            throws TransformerFactoryConfigurationError, TransformerException {
        Source source = new DOMSource(doc);

        // Write the DOM document to the file
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "ASCII");
        transformer.transform(source, result);
    }
}

Related

  1. saveDocAsXML(Document doc, String filename)
  2. saveDocument(Document doc, File f)
  3. saveDocument(Document doc, File file)
  4. saveDocument(Document doc, File file)
  5. saveDocument(Document doc, String fullFileName)
  6. saveDocument(Document document, File outputFile)
  7. saveDocument(final Document doc, String encoding, String docPath)
  8. saveDocument(final Document document, final File destination)
  9. saveDocument(final Document document, String filename)