Java XML Document Save to File saveDocument(final Document document, String filename)

Here you can find the source of saveDocument(final Document document, String filename)

Description

save Document

License

Open Source License

Declaration

public static void saveDocument(final Document document, String filename) throws TransformerException 

Method Source Code


//package com.java2s;
/*//from  w  w  w  .ja v a 2s. co  m
 * Forge: Play Magic: the Gathering.
 * Copyright (C) 2011  Forge Team
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import org.w3c.dom.Document;

import javax.xml.transform.OutputKeys;
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 java.io.File;

public class Main {
    public static void saveDocument(final Document document, String filename) throws TransformerException {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(new File(filename));
        t.transform(source, result);
    }
}

Related

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