Java XML Document Save to File saveDocumentToFile(Document doc, File file)

Here you can find the source of saveDocumentToFile(Document doc, File file)

Description

save Document To File

License

Open Source License

Declaration

public static void saveDocumentToFile(Document doc, File file) throws Exception 

Method Source Code

//package com.java2s;

import java.io.File;

import java.io.FileOutputStream;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
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 {
    public static void saveDocumentToFile(Document doc, File file) throws Exception {
        if (file.getParent() != null) {
            mkdirs(file.getParent());// ww w.  j a  v  a  2  s .c om
        }
        FileOutputStream fos = new FileOutputStream(file);
        Source source = new DOMSource(doc);
        Result result = new StreamResult(fos);
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        xformer.transform(source, result);
        fos.close();
    }

    public static void mkdirs(String pathName) {
        File file = new File(pathName);
        file.mkdirs();
    }
}

Related

  1. saveDocument(Document document, File outputFile)
  2. saveDocument(final Document doc, String encoding, String docPath)
  3. saveDocument(final Document document, final File destination)
  4. saveDocument(final Document document, String filename)
  5. saveDocument(String filename, byte[] document)
  6. saveDocumentToFile(Document doc, String filename)
  7. saveDOM(Document doc, String filename)
  8. SaveDOM(Document document, String filename)
  9. SaveDom(Document dom, String filepath)