Java XML String Transform writeXmlFile(Element doc, String filename)

Here you can find the source of writeXmlFile(Element doc, String filename)

Description

Writes an XML element to a given file

License

Apache License

Parameter

Parameter Description
doc XML element
filename Filename of the file where to write XML

Declaration

public static void writeXmlFile(Element doc, String filename) 

Method Source Code


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

import java.io.File;

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.Element;

public class Main {
    /**/*from www.ja va2s . c  om*/
     * Writes an XML element to a given file
     * @param doc XML element
     * @param filename Filename of the file where to write XML
     */
    public static void writeXmlFile(Element doc, String filename) {
        try {

            Source source = new DOMSource(doc);
            File file = new File(filename);
            Result result = new StreamResult(file);

            Transformer xformer = TransformerFactory.newInstance().newTransformer();
            xformer.transform(source, result);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. writePropToString(Properties props)
  2. writeResultToFile(final StreamResult result, final String path)
  3. writeToFile(String xmlContent, String path)
  4. writeXML(Node node, String dtdFilename, String outputFileName)
  5. writeXml(OutputStream os, Node node, String encoding)
  6. writeXmlResult(String xml, Writer w)
  7. xslString(final String xmlFile, final InputStream xslStream)