Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OutputStreamWriter java can't store into string

transformer.transform(new DOMSource(doc),new StreamResult(new OutputStreamWriter(System.out, "UTF-8")));

I'm using this to generate the output to console. I want to store the output to a sting. I don't know how to do. Can any one help me? :(

Thanks in advance

like image 688
nixon1333 Avatar asked May 19 '26 15:05

nixon1333


2 Answers

For example use this (it avoids encoding the string):

StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
String str = writer.getBuffer().toString();
like image 64
clearwater Avatar answered May 22 '26 04:05

clearwater


Use StringWriter class instead of OutputStreamWriter.

like image 29
Hakan Serce Avatar answered May 22 '26 05:05

Hakan Serce



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!