Java XML Document Save to File saveXml(Document modDoc, String path)

Here you can find the source of saveXml(Document modDoc, String path)

Description

save Xml

License

Apache License

Declaration

public static void saveXml(Document modDoc, String path) throws TransformerException, IOException 

Method Source Code


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

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
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 saveXml(Document modDoc, String path) throws TransformerException, IOException {
        Writer writer = null;/*from   w w w. j  a v  a2s  .c  o m*/
        try {
            TransformerFactory tranFactory = TransformerFactory.newInstance();
            Transformer aTransformer = tranFactory.newTransformer();
            Source src = new DOMSource(modDoc);
            writer = getFileWriter(path);
            Result dest = new StreamResult(writer);

            aTransformer.setOutputProperty(OutputKeys.INDENT, "yes");
            aTransformer.setOutputProperty(OutputKeys.METHOD, "xml");
            aTransformer.transform(src, dest);
        } catch (TransformerException e) {
            throw e;
        } finally {

            writer.close();
        }

    }

    public static FileWriter getFileWriter(String filePath) {

        File xmlFile = new File(filePath);
        FileWriter writer = null;
        try {
            writer = new FileWriter(xmlFile);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return writer;
    }
}

Related

  1. saveXML(Document doc, ByteArrayOutputStream outputStreamXML)
  2. saveXML(Document doc, File outfile, boolean indent)
  3. saveXml(Document doc, String filename)
  4. saveXML(Document document, String filename)
  5. saveXml(Document dom, String filePath)
  6. saveXMLDocument(Document document, String path)
  7. saveXMLDocument(String fileName, Document doc)
  8. saveXmlToStreamWriter(Document doc)