Java XML Document to File writeXmlFile(File file, Document document)

Here you can find the source of writeXmlFile(File file, Document document)

Description

write Xml File

License

Apache License

Declaration

public final static synchronized long writeXmlFile(File file, Document document) throws Exception 

Method Source Code


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

import java.io.File;

import java.util.Properties;

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

public class Main {
    public final static synchronized long writeXmlFile(File file, Document document) throws Exception {
        DOMSource doms = new DOMSource(document);
        StreamResult result = new StreamResult(file);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        Properties properties = transformer.getOutputProperties();
        properties.setProperty(OutputKeys.ENCODING, "utf-8");
        properties.setProperty(OutputKeys.METHOD, "xml");
        properties.setProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperties(properties);
        transformer.transform(doms, result);
        return 0;
    }/*  w  w  w. jav  a  2s . co m*/
}

Related

  1. writeXmlFile(Document doc, File file, boolean indent, String encoding)
  2. writeXmlFile(Document doc, String filename)
  3. writeXmlFile(Document doc, String filename)
  4. writeXmlFile(Document doc, String filename)
  5. writeXMLFile(Document document, Writer writer)
  6. writeXmlFile(String fileName, Document document)
  7. WriteXMLFile2(Document doc, String strFilePath)
  8. writeXmlToStream(Document doc, OutputStream stream)
  9. writeXMLtoStream(Object osw, Document doc)