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

我正在使用它来生成控制台的输出。我想将输出存储到一个刺痛中。我不知道该怎么做。谁能帮我?:(

提前致谢

4

3 回答 3

7

例如使用这个(它避免编码字符串):

StringWriter writer = new StringWriter();
transformer.transform(new DOMSource(doc), new StreamResult(writer));
String str = writer.getBuffer().toString();
于 2013-06-23T21:57:00.290 回答
2

使用StringWriter类而不是OutputStreamWriter.

于 2013-06-23T21:55:27.853 回答
0

一种解决方案可能如下所示:

ByteArrayOutputStream out = new ByteArrayOutputStream();
transformer.transform(new DOMSource(doc),new StreamResult(new OutputStreamWriter(out, "UTF-8")));
String string = new String(out.toByteArray(), "UTF-8");
于 2013-06-23T21:54:01.723 回答