Welcome to Yumao′s Blog.
最近有用到jox工具類
這個類的主要功能就是將xml和javabean對象互相轉換
但是早上碰到一個問題
將xml轉換成bean之後中文亂碼
網上瘋找發現全都是bean轉xml亂碼解決
那麼這裏就提一下解決方式好了
在避免修改工具類代碼的情況下
將stream先編碼再調用jox即可避免亂碼產生
public void xmlJOX(String xmlDoc){
try{
// xmlDoc = new String(xmlDoc.getBytes("ISO-8859-1"), "GBK");
// ByteArrayInputStream stream = new ByteArrayInputStream(xmlDoc.getBytes("GBK"));
ByteArrayInputStream stream = new ByteArrayInputStream(xmlDoc.getBytes("ISO-8859-1"));
JOXBeanInputStream joxIn = new JOXBeanInputStream(stream);
GameChargeResponse gcr = (GameChargeResponse) joxIn.readObject(GameChargeResponse.class);
System.out.println(gcr);
}
catch (Exception exc){
exc.printStackTrace();
}
}
順便給出bean轉xml亂碼解決方式:
JOXBeanOutputStream joxOut =new JOXBeanOutputStream(fileOut,"UTF-8");