xml – 如何使用XStream框架对UTF-8进行编码?

前端之家收集整理的这篇文章主要介绍了xml – 如何使用XStream框架对UTF-8进行编码?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
根据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);
原文链接:https://www.f2er.com/xml/292811.html

猜你在找的XML相关文章