Welcome to Yumao′s Blog.
关于JOX转换Bean时乱码解决
, 2012年12月14日 , Java Language , 评论 在〈关于JOX转换Bean时乱码解决〉中留言功能已關閉 ,

最近有用到jox工具類
這個類的主要功能就是將xml和javabean對象互相轉換
但是早上碰到一個問題
將xml轉換成bean之後中文亂碼
網上瘋找發現全都是bean轉xml亂碼解決
那麼這裏就提一下解決方式好了
在避免修改工具類代碼的情況下
將stream先編碼再調用jox即可避免亂碼產生

1
2
3
4
5
6
7
8
9
10
11
12
13
    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亂碼解決方式:

1
JOXBeanOutputStream joxOut =new JOXBeanOutputStream(fileOut,"UTF-8");
关键字:, , , ,

评论已关闭