Java XML Document to File writeDocumentToFile(Document document, String filePathname, String method, int indent)

Here you can find the source of writeDocumentToFile(Document document, String filePathname, String method, int indent)

Description

Write the document object to a file.

License

Apache License

Parameter

Parameter Description
document the document object.
filePathname the path name of the file to be written to.
method the output method: for instance html, xml, text
indent amount of indentation. -1 to use the default.

Exception

Parameter Description
TransformerException if an exception occurs.
IOException if an IO exception occurs.

Declaration

public static void writeDocumentToFile(Document document, String filePathname, String method, int indent)
        throws TransformerException, IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.FileOutputStream;
import java.io.IOException;

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 org.w3c.dom.Document;

public class Main {
    /**/*  w ww .j a  va  2  s .c o m*/
     * Write the document object to a file.
     * 
     * @param document
     *            the document object.
     * @param filePathname
     *            the path name of the file to be written to.
     * @param method
     *            the output method: for instance html, xml, text
     * @param indent
     *            amount of indentation. -1 to use the default.
     * @throws TransformerException
     *             if an exception occurs.
     * @throws IOException
     *             if an IO exception occurs.
     */
    public static void writeDocumentToFile(Document document, String filePathname, String method, int indent)
            throws TransformerException, IOException {

        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        transformer.setOutputProperty(OutputKeys.METHOD, method);

        transformer.transform(new DOMSource(document), new StreamResult(new FileOutputStream(filePathname)));
    }
}

Related

  1. writeDocument(Document doc, String filePath)
  2. writeDocument(Document document, File file)
  3. writeDocument(Document document, File file)
  4. writeDocument(Document document, String path)
  5. writeDocumentTo(Document doc, File file)
  6. writeDOM2XMLFile(Document domDoc, String fileName)
  7. writeDOMDocumentToFile(Document doc, String filePath)
  8. writeDomToFile(Document doc, String filename)
  9. writeDomToFile(Document dom, File outFile)