Output XML tree to file

I often forget it, so make a note How to receive doc and output as xml to file

public static void export(Document doc, File file) throws TransformerException {
    TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer = transFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    Source src = new DOMSource(doc);
    Result result = new StreamResult(file);
    transformer.transform(src, result);
}

Recommended Posts

Output XML tree to file
Output javadoc to word file
Log output to file in Java
[Spring Batch] Output table data to CSV file
Output log to external file with slf4j + logback with Maven
Gzip-compress byte array in Java and output to file
csv file output with opencsv
Output JaCoCo coverage to console
I made a tool to output the difference of CSV file
Add Extended Attributes to the file
SCP transfer War file to Tomcat
Introduction to swift practice output Chapter5
Generate HashMap from XML resource file
How to convert erb file to haml
[Beginner] How to delete NO FILE
ERESOLVE unable to resolve dependency tree
Enable log output to both file and console using log4j in Eclipse.
How to record JFR (Java Flight Recorder) and output a dump file