transformer.transform(new DOMSource(doc),new StreamResult(new OutputStreamWriter(System.out, "UTF-8")));
我正在使用它来生成控制台的输出。我想将输出存储到一个刺痛中。我不知道该怎么做。谁能帮我?:(
提前致谢
transformer.transform(new DOMSource(doc),new StreamResult(new OutputStreamWriter(System.out, "UTF-8")));
我正在使用它来生成控制台的输出。我想将输出存储到一个刺痛中。我不知道该怎么做。谁能帮我?:(
提前致谢
例如使用这个(它避免编码字符串):
StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
String str = writer.getBuffer().toString();
使用StringWriter
类而不是OutputStreamWriter
.
一种解决方案可能如下所示:
ByteArrayOutputStream out = new ByteArrayOutputStream();
transformer.transform(new DOMSource(doc),new StreamResult(new OutputStreamWriter(out, "UTF-8")));
String string = new String(out.toByteArray(), "UTF-8");