转载:原帖地址
解析xml报文时,中文处出现乱码[【查询成功!】
引用:
public static String unicodeToString(String sourceString) {
// 定义正则表达式来搜索中文字符的转义符号
Pattern compile = Pattern.compile("&#.*?;");
// 测试用中文字符
//String sourceString = "查询成功!";
Matcher matcher = compile.matcher(sourceString);
// 循环搜索 并转换 替换
while (matcher.find()) {
String group = matcher.group();
// 获得16进制的码
String hexcode = "0" + group.replaceAll("(&#|;)", "");
// 字符串形式的16进制码转成int并转成char 并替换到源串中
sourceString = sourceString.replaceAll(group, (char) Integer.decode(hexcode).intValue() + "");
}
return sourceString;
}