Java XML Document Save to File saveDOM(Document doc, String filename)

Here you can find the source of saveDOM(Document doc, String filename)

Description

Saves a document object model (DOM) as a file with the chosen file name.

License

Open Source License

Parameter

Parameter Description
doc DOM representation of XML instance.
filename The file to which the DOM should be saved. The String must conform to the URI syntax.

Return

true if the file could be saved; false if the file could not be saved.

Declaration

public static boolean saveDOM(Document doc, String filename) 

Method Source Code

//package com.java2s;
/**//from  w  ww  . ja  va  2 s.com
 *  odt2daisy - OpenDocument to DAISY XML/Audio
 *
 *  (c) Copyright 2008 - 2012 by Vincent Spiewak, All Rights Reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Lesser Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;

import javax.xml.transform.OutputKeys;
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 {
    private static final Logger logger = Logger.getLogger("com.versusoft.packages.jodl.odtutils");

    /**
     * Saves a document object model (DOM) as a file with the chosen file name.
     * 
     * @param doc DOM representation of XML instance.
     * @param filename The file to which the DOM should be saved. The String must conform to the URI syntax.
     * @return true if the file could be saved; false if the file could not be saved.
     */
    public static boolean saveDOM(Document doc, String filename) {
        boolean save = false;
        try {

            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3");

            StreamResult result = new StreamResult(filename);
            DOMSource source = new DOMSource(doc);
            transformer.transform(source, result);

            save = true;
        } catch (TransformerConfigurationException ex) {
            logger.log(Level.SEVERE, null, ex);
        } catch (TransformerException ex) {
            logger.log(Level.SEVERE, null, ex);
        } finally {

            return save;

        }
    }
}

Related

  1. saveDocument(final Document document, final File destination)
  2. saveDocument(final Document document, String filename)
  3. saveDocument(String filename, byte[] document)
  4. saveDocumentToFile(Document doc, File file)
  5. saveDocumentToFile(Document doc, String filename)
  6. SaveDOM(Document document, String filename)
  7. SaveDom(Document dom, String filepath)
  8. saveDOM(final Document doc, final OutputStream output)
  9. saveDomToFile(Document doc, String filePath)