Java XML Document Save to File saveReportToFile(Node node, String documentFileName)

Here you can find the source of saveReportToFile(Node node, String documentFileName)

Description

save Report To File

License

Apache License

Declaration

public static void saveReportToFile(Node node, String documentFileName) 

Method Source Code


//package com.java2s;
//License from project: Apache 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.Node;

public class Main {
    public static void saveReportToFile(Node node, String documentFileName) {
        // StringWriter writer = new StringWriter();
        // StreamResult resultString = new StreamResult(writer);

        File output = new File(documentFileName);
        StreamResult resultFile = new StreamResult(output);

        DOMSource source = new DOMSource(node);
        try {//  ww w. j  av  a2  s. c o  m
            Transformer t = TransformerFactory.newInstance().newTransformer();
            t.setOutputProperty(OutputKeys.INDENT, "yes");
            t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            t.setOutputProperty(OutputKeys.METHOD, "xml");
            t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
            t.transform(source, resultFile);
            // t.transform(source, resultString);
        } catch (TransformerException e) {
            e.printStackTrace();
        }
        // return writer.toString();
    }
}

Related

  1. SaveDom(Document dom, String filepath)
  2. saveDOM(final Document doc, final OutputStream output)
  3. saveDomToFile(Document doc, String filePath)
  4. saveHumanReadable(Document document, File file)
  5. saveImpl(Document document, StreamResult output, Properties outputProperties)
  6. saveTemporaryDocument(Document document, String folderName)
  7. saveToDisk(Document doc, OutputStream out)
  8. saveToFile(Document doc, File f, boolean indent)
  9. saveToFile(String filename, Document document)