Java XML Document Save to File saveHumanReadable(Document document, File file)

Here you can find the source of saveHumanReadable(Document document, File file)

Description

Saves a DOM to a human-readable XML file (4-space indentation, UTF-8).

License

LGPL

Parameter

Parameter Description
document The DOM
file The target XML file

Exception

Parameter Description
TransformerFactoryConfigurationError In case of an XML transformation factory configuration error
TransformerException In case of an XML transformation error
IOException In case of an I/O error

Declaration

public static void saveHumanReadable(Document document, File file)
        throws TransformerFactoryConfigurationError, TransformerException, IOException 

Method Source Code

//package com.java2s;
/**/*from   w  w w  .  jav  a2  s.  com*/
 * Copyright 2011-2016 Three Crickets LLC.
 * <p>
 * The contents of this file are subject to the terms of the LGPL version 3.0:
 * http://www.gnu.org/copyleft/lesser.html
 * <p>
 * Alternatively, you can obtain a royalty free commercial license with less
 * limitations, transferable or non-transferable, directly from Three Crickets
 * at http://threecrickets.com/
 */

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import javax.xml.transform.OutputKeys;
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 {
    /**
     * Saves a DOM to a human-readable XML file (4-space indentation, UTF-8).
     * <p>
     * Contains workaround for various JVM bugs.
     * 
     * @param document
     *        The DOM
     * @param file
     *        The target XML file
     * @throws TransformerFactoryConfigurationError
     *         In case of an XML transformation factory configuration error
     * @throws TransformerException
     *         In case of an XML transformation error
     * @throws IOException
     *         In case of an I/O error
     */
    public static void saveHumanReadable(Document document, File file)
            throws TransformerFactoryConfigurationError, TransformerException, IOException {
        // Various indentation and UTF8 encoding bugs are worked around here
        TransformerFactory factory = TransformerFactory.newInstance();
        factory.setAttribute("indent-number", new Integer(4));
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), "UTF8");
        transformer.transform(new DOMSource(document), new StreamResult(writer));
        writer.close();
    }
}

Related

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