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

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

Description

save To File

License

Open Source License

Declaration

public static File saveToFile(String filename, Document document) throws TransformerException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import org.w3c.dom.Document;

import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;

public class Main {
    public static File saveToFile(String filename, Document document) throws TransformerException {
        // Prepare the DOM document for writing
        Source source = new DOMSource(document);

        // Prepare the output file
        File file = new File(filename);
        Result result = new StreamResult(file);

        // Write the DOM document to the file
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        xformer.transform(source, result);

        return file;
    }/*from  w  ww.  j av a  2  s.  co  m*/
}

Related

  1. saveImpl(Document document, StreamResult output, Properties outputProperties)
  2. saveReportToFile(Node node, String documentFileName)
  3. saveTemporaryDocument(Document document, String folderName)
  4. saveToDisk(Document doc, OutputStream out)
  5. saveToFile(Document doc, File f, boolean indent)
  6. saveToXml(Document doc, OutputStream os)
  7. saveToXmlStr(Document doc)
  8. saveXML(Document doc, ByteArrayOutputStream outputStreamXML)
  9. saveXML(Document doc, File outfile, boolean indent)