How do I encode UTF-8 using the XStream framework?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Per XStream's FAQ its default parser does not preserve UTF-8 document encoding, and one must provide their own encoder. How does one do this? Thanks! 回答1: Create a Writer with UTF-8 encoding. Pass the Writer as an argument to XStream's toXML method. XStream xstream = new xStream(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Writer writer = new OutputStreamWriter(outputStream, "UTF-8"); xStream.toXML(object, writer); String xml = outputStream.toString("UTF-8"); You may also use that Writer to include the XML Declaration