Java: Saving StreamResult to a file

Todd picture Todd · Dec 21, 2009 · Viewed 23.5k times · Source

I am doing some data conversion(like csv) to xml with SAX then using transformer in Java. The result is in StreamResult, and I am trying to save this result to a file.xml but I can't find way to save StreamResult into file. am I doing this all wrong?

Answer

Carl Smotricz picture Carl Smotricz · Dec 21, 2009

Your StreamResult should be created on the basis of a file, e.g.

StreamResult sr = new StreamResult(new File("/my/file.xml"));

If you give your Transformer such a StreamResult, it will write its result directly into the file you specified.