{
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = null;
try {
transformer = factory.newTransformer();
} catch (TransformerConfigurationException e) {
throw new RuntimeException("Unhandled exception " + e.toString());
}
Properties outFormat = new Properties();
outFormat.setProperty(OutputKeys.INDENT, "yes");
outFormat.setProperty(OutputKeys.METHOD, "xml");
outFormat.setProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
outFormat.setProperty(OutputKeys.VERSION, "1.0");
outFormat.setProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperties(outFormat);
DOMSource domSource = new DOMSource(doc);
OutputStream output = null;
try {
output = new FileOutputStream(new File(iDir, "image.xml"));
} catch (FileNotFoundException e) {
throw new RuntimeException("Unhandled exception " + e.toString());
}
StreamResult result = new StreamResult(output);
try {
transformer.transform(domSource, result);
output.flush();
} catch (TransformerException e) {
throw new RuntimeException("Unhandled exception " + e.toString());
} catch (IOException e) {
throw new RuntimeException("Unhandled exception " + e.toString());
} finally {
try {
output.close();
} catch (IOException e) {
throw new RuntimeException("Unhandled exception " + e.toString());
}
}
}