Java XML Document to File writeDocToFile(Document doc, String fileName)

Here you can find the source of writeDocToFile(Document doc, String fileName)

Description

* Output Document to file ******************************************.

License

Open Source License

Parameter

Parameter Description
doc the doc
fileName the file name

Exception

Parameter Description
Exception the exception

Declaration

public static void writeDocToFile(Document doc, String fileName) throws Exception 

Method Source Code


//package com.java2s;
import org.w3c.dom.Document;
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 java.io.File;
import java.io.FileOutputStream;

public class Main {
    /**/*from   ww  w .j a v a 2 s.  co  m*/
     * *****************************************
     * Output Document to file
     * ******************************************.
     *
     * @param doc the doc
     * @param fileName the file name
     * @throws Exception the exception
     */
    public static void writeDocToFile(Document doc, String fileName) throws Exception {
        File encryptionFile = new File("c:\\temp\\" + fileName);
        FileOutputStream f = new FileOutputStream(encryptionFile);

        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(f);
        transformer.transform(source, result);

        f.close();
    }
}

Related

  1. write(final Document doc, final File file)
  2. writeDataToFile(File file, Document doc)
  3. writeDoc(Document doc, File aFile)
  4. writeDoc(Document doc, File file)
  5. writeDocToFile(Document doc, String filename)
  6. writeDocument(Document doc, String filename)
  7. writeDocument(Document doc, String filePath)
  8. writeDocument(Document doc, String filePath)
  9. writeDocument(Document document, File file)