Java XML Document to File writeXMLDocument(Document doc, String filename)

Here you can find the source of writeXMLDocument(Document doc, String filename)

Description

Writes the XML document to the particular file specified as argument

License

Open Source License

Parameter

Parameter Description
doc the document
filename the path to the file in which to write the XML data writing operation fails

Declaration

public static void writeXMLDocument(Document doc, String filename) 

Method Source Code


//package com.java2s;
// publish, distribute, sublicense, and/or sell copies of the Software, 

import java.util.logging.*;
import java.io.File;

import javax.xml.transform.OutputKeys;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
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 {
    final static Logger log = Logger.getLogger("OpenDial");

    /**/*from  w  ww  .  j av  a 2 s.  com*/
     * Writes the XML document to the particular file specified as argument
     * 
     * @param doc the document
     * @param filename the path to the file in which to write the XML data writing
     *            operation fails
     */
    public static void writeXMLDocument(Document doc, String filename) {
        try {
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer;
            transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new File(filename));
            transformer.transform(source, result);
            log.fine("writing operation to " + filename + " successful!");
        } catch (TransformerConfigurationException e) {
            log.warning(e.getMessage());
        } catch (TransformerException e) {
            log.warning(e.getMessage());
        }
    }
}

Related

  1. writeXml(Document doc, File output)
  2. writeXml(Document doc, File outputFile)
  3. writeXML(Document doc, String fileName)
  4. writeXML(final File file, final Document doc)
  5. writeXML(String OUTPUT_XML_FILE, org.w3c.dom.Document xmlDoc)
  6. writeXMLDocumentToFile(Document doc, String outputFilename)
  7. writeXmlFile(Document doc, File file)
  8. writeXmlFile(Document doc, File file)
  9. writeXmlFile(Document doc, File file, boolean indent, String encoding)