根据XStream的FAQ,其默认解析器不保留UTF-8文档编码,并且必须提供自己的编码器.怎么做这个?
谢谢!
使用UTF-8编码创建Writer.将Writer作为参数传递给XStream的to
XML方法.
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");
您也可以使用该Writer来包含XML声明.
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"); xStream.toXML(object,writer);