Java XML Document to File storeXml(final File xmlFile, final Document document)

Here you can find the source of storeXml(final File xmlFile, final Document document)

Description

store Xml

License

Open Source License

Declaration

public static void storeXml(final File xmlFile, final Document document)
        throws TransformerException 

Method Source Code

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

import java.io.File;

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 {

    public static void storeXml(final File xmlFile, final Document document)
            throws TransformerException {
        if (xmlFile == null) {
            throw new NullPointerException("xmlFile");
        }/* www.j a  v  a 2 s.  c om*/
        if (xmlFile.exists() && !xmlFile.canWrite()) {
            throw new IllegalArgumentException("xmlFile");
        }
        if (xmlFile.exists() && !xmlFile.isFile()) {
            throw new IllegalArgumentException("xmlFile");
        }
        if (document == null) {
            throw new NullPointerException("document");
        }

        StreamResult result = new StreamResult(xmlFile);
        DOMSource source = new DOMSource(document);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.transform(source, result);
    }
}

Related

  1. dumpDocument(Document doc, String outName)
  2. storeXML(Document doc, File file)
  3. write(Document doc, File out)
  4. write(final Document doc, final File file)
  5. writeDataToFile(File file, Document doc)
  6. writeDoc(Document doc, File aFile)