Posted By:
Ali_Taheri
Posted On:
Monday, October 18, 2004 04:56 PM
I have an XML file that I parsed using a DOMParser and now have a document object. Now I want to write that document object to an XML file and have it be formatted nicely (tabs, line feeds, etc). I accomplished what I needed by using a Transformer. I'm interested in what people think of the following two approaches and using one versus the other. The first approach uses . This approach was presented in "Java and XML" by McLaughlin. You can download the code from the chapter 5 eamples at http://newinstance.com/writing/javaxml2_examples/javaxml2_ch05.zip It requires handling each node and seemed especially cumbersome after I found out about the next approach. In the second ap
More>>
I have an XML file that I parsed using a DOMParser and now have a document object. Now I want to write that document object to an XML file and have it be formatted nicely (tabs, line feeds, etc). I accomplished what I needed by using a Transformer. I'm interested in what people think of the following two approaches and using one versus the other.
The first approach uses
. This approach was presented in "Java and XML" by McLaughlin. You can download the code from the chapter 5 eamples at http://newinstance.com/writing/javaxml2_examples/javaxml2_ch05.zip
It requires handling each node and seemed especially cumbersome after I found out about the next approach.
In the second approach I use
and can write the document to file with the following code (based on http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JAXPXSLT4.html):
...
...
// Use a Transformer for output
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
FileWriter outFile = new FileWriter(outFileName);
StreamResult result = new StreamResult(outFile);
transformer.transform(source, result);
...
...
<<Less