我需要将一些来自HashMap的数据放入xml中。但是Windows1251编码存在一个问题--它使用"т*;ри*;“等标记返回xml。下面是带有cp1251值的测试hashMap代码:
public String getValues() {
XmlSerializer serializer = Xml.newSerializer();
OutputStream outputStream = new ByteArrayOutputStream();
HashMap<String, String> map = new HashMap<String, String>();
map.put("one", "один");
map.put("two", "два");
map.put("three", "три");
try {
serializer.setOutput(outputStream, "Windows-1251");
serializer.startTag("", "fields");
for (String key : map.keySet()) {
serializer.startTag("", key);
serializer.text(map.get(key));
serializer.endTag("", key);
}
serializer.endTag("", "fields");
serializer.endDocument();
} catch (Exception e) {
Log.e(TAG, e.toString);
}
return outputStream.toString();
}如何正确设置编码?谢谢
发布于 2014-11-18 08:08:14
我找到了解决方案(我使用了错误的编码):
serializer.setOutput(outputStream, "UTF-8");现在一切都很好
https://stackoverflow.com/questions/26980773
复制相似问题