I want to save my DOM Document as an XML file. I follow this tutorial: http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JAXPXSLT4.html
So, this is my code:
...
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResu开发者_开发技巧lt result = new StreamResult(System.out);
transformer.transform(source, result);
but instead of System.out, I want to save in a file the result. How can I do this?
Use
new StreamResult(new FileOutputStream(...))
But you may want to use a Writer
, so that you are outputting encoded characters, unless StreamResult
is using a Unicode encoding, say UTF-8, implicitly.
精彩评论