使用xStream进行java object<-->xml之间的转换

官方网站:http://xstream.codehaus.org/

测试了一下,的确十分方便。

java 代码
  1. public static void write() {   
  2.     XStream sm = new XStream();   
  3.     mytest t = new mytest();   
  4.     t.setName("moogle");   
  5.     t.setXb("男");   
  6.     try {   
  7.     FileOutputStream ops = new FileOutputStream(new File("C:\\111.xml"));   
  8.     sm.toXML(t, ops);   
  9.     ops.close();   
  10.     } catch (Exception e) {   
  11.         e.printStackTrace();   
  12.     }   
  13. }      
  14. public static void read() {   
  15.     XStream sm = new XStream(new DomDriver());   
  16.     try {   
  17.         FileInputStream ops = new FileInputStream(new File("C:\\111.xml"));   
  18.         mytest t = (mytest)sm.fromXML(ops);   
  19.         System.out.println(t.getName());   
  20.         ops.close();   
  21.         } catch (Exception e) {   
  22.             e.printStackTrace();   
  23.         }          

生成的XML文件内容:

xml 代码
  1. <mytest>  
  2.   <name>asd</name>  
  3.   <xb>男</xb>  
  4. </mytest>  


问题:
1.生成的xml文档没有

2.如果生成的文档中含有中文,比如上文代码中setXb("男")
在读取的时候会报
[Fatal Error] :4:7: Invalid byte 2 of 2-byte UTF-8 sequence.

请指教。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值