1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| private void writeDOM2XmlFile(Document documentDOM, String filename) {
try {
// Prepare the DOM document for writing
Source source = new DOMSource(documentDOM);
// Prepare the output file
File file = new File(filename);
Result result = new StreamResult(file.toURI().getPath());
// Write the DOM document to the file
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.transform(source, result);
} catch(TransformerConfigurationException e) {
e.printStackTrace();
} catch(TransformerException e) {
e.printStackTrace();
}
} |