private void writeXMLDocument(final Document doc, final String fileName) throws IOException {
// open output stream where XML Document will be saved
File xmlOutputFile = new File(fileName);
FileOutputStream fos;
Transformer transformer;
fos = new FileOutputStream(xmlOutputFile);
// Use a Transformer for output
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setAttribute("indent-number", new Integer(4));
try {
transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "-//Hibernate/Hibernate Mapping DTD 3.0//EN");
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,
transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
} catch (TransformerConfigurationException e) {
logger.error("Transformer configuration error: " + e.getMessage());
throw new IOException(e.getMessage());
}
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new OutputStreamWriter(fos, "utf-8"));
// transform source into result will do save
try {
transformer.transform(source, result);
} catch (TransformerException e) {
logger.error("Error transform: " + e.getMessage());
}
}
No comments:
Post a Comment