Java XML Document Save to File saveTemporaryDocument(Document document, String folderName)

Here you can find the source of saveTemporaryDocument(Document document, String folderName)

Description

save Temporary Document

License

Mozilla Public License

Declaration

private static void saveTemporaryDocument(Document document, String folderName) throws IOException, Exception 

Method Source Code


//package com.java2s;
//License from project: Mozilla Public License 

import java.io.BufferedReader;
import java.io.File;

import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

import org.w3c.dom.Document;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSSerializer;

public class Main {
    private static int index = 0;

    private static void saveTemporaryDocument(Document document, String folderName) throws IOException, Exception {
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer writer = impl.createLSSerializer();

        String str = writer.writeToString(document);
        str = str.replace("UTF-16", "UTF-8");

        File outputFolder = new File(folderName);
        outputFolder.mkdirs();/*from   ww w  .j  av  a  2  s .c  om*/

        String fileName = outputFolder.getAbsolutePath() + "/" + "file_" + String.valueOf(index) + ".xml";

        OutputStreamWriter outWriter = null;
        try {
            outWriter = new OutputStreamWriter(new FileOutputStream(fileName), "UTF-8");
            outWriter.write(str);
        } finally {
            if (outWriter != null) {
                outWriter.close();
            }

        }

        index++;
    }

    /**
     * TODO Description.
     * 
     * @param fileName
     * @param destination
     */
    public static void write(File fileName, String destination) throws IOException {
        File outputFolder = new File(destination);
        outputFolder.mkdirs();
        Writer outWriter = null;
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
            outWriter = new OutputStreamWriter(new FileOutputStream(outputFolder), "UTF-8");
            String line = null;
            while ((line = bufferedReader.readLine()) != null) {
                outWriter.write(line);
            }
        } finally {
            if (outWriter != null) {
                outWriter.close();
            }
        }
    }
}

Related

  1. saveDOM(final Document doc, final OutputStream output)
  2. saveDomToFile(Document doc, String filePath)
  3. saveHumanReadable(Document document, File file)
  4. saveImpl(Document document, StreamResult output, Properties outputProperties)
  5. saveReportToFile(Node node, String documentFileName)
  6. saveToDisk(Document doc, OutputStream out)
  7. saveToFile(Document doc, File f, boolean indent)
  8. saveToFile(String filename, Document document)
  9. saveToXml(Document doc, OutputStream os)