20060613 Tuesday June 13, 2006

Serializing a DOM Document to a File

Here is a simple way to write a DOM document to an XML file:

Document document = ...
String filename = ...

Source source = new DOMSource(document);
File file = new File(filename);
Result result = new StreamResult(file);
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform(source, result);

This approach actually works for writing XML to just about any type of stream. Just pass it to the constructor for the StreamResult object.

 

Posted by rickg ( Jun 13 2006, 12:06:17 AM PDT ) Permalink Comments [0]