How to write XML document?
Hi,
I am referring to javax.xml.parsers.DocumentBuilder class.
This class has a method "parse" which returns us the Document object.
My question is how can I write that Document back to file system?
Is there any class which can write the XML document to file system?
Thanks.
[311 byte] By [
gsoapa] at [2007-11-27 4:48:09]

Let doc be an instance of org.w3c.dom.Document, let fn be a String denoting a destination file, then you can try something along the lines of:final TransformerFactory tfactory = TransformerFactory.newInstance();
try {
try {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fn), "UTF-8"));
final Transformer transformer = tfactory.newTransformer();
transformer.transform(new DOMSource(doc), new StreamResult(bw));
bw.flush();
} finally {
if (bw != null) {
bw.close();
}
}
} catch (IOException ioe) {
// todo
}
} catch (TransformerConfigurationException tce) {
// todo
} catch (TransformerException te) {
// todo
}
(Nervermind the uneccessarily nested try/catch blocks - I removed some unneccessary stuff and was too lazy to condense these)