官方网站: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.

请指教。

评论
david3d 2007-07-16
Thanks. JJYAO.
I have replied you several days before but it was deleted by forum manager. Maybe because I pasted some redundant code lines in that reply : (

Your solution still cannot resolve my problem. But give me a good directive. I will try another way. Thanks again
JJYAO 2007-07-12
david3d 写道
The same question as above to JJYAO...
How to remove the reference of cglib?

Currently I encounter an issue while converting a hibernate set property to Json(I think XML should have same problem). Could u explain the solution to us?

Thank u very much in advance.


Version: xstream 1.2.1
To make comments on XStream.java from line 635 to 640, then re-compile


//        if (jvm.loadClass("net.sf.cglib.proxy.Enhancer") != null) {
//            dynamicallyRegisterConverter(
//                    "com.thoughtworks.xstream.converters.reflection.CGLIBEnhancedConverter",
//                    PRIORITY_NORMAL, new Class[]{Mapper.class, ReflectionProvider.class}, 
//                    new Object[]{mapper, reflectionProvider});
//        }
wtusmchen 2007-07-12
支持xStream,一直用它!好使!!
david3d 2007-07-11
The same question as above to JJYAO...
How to remove the reference of cglib?

Currently I encounter an issue while converting a hibernate set property to Json(I think XML should have same problem). Could u explain the solution to us?

Thank u very much in advance.
teligen 2007-06-27
JJYAO,how to remove the reference of cglib?
oksonic 2007-04-16
fire使用的却是xmlbean
生命火花 2007-04-16
已经习惯了用JAXB了!各方面都还满意
JAVA_ED 2007-04-16
JJYAO 写道
I find another compatible problem today.

    
    Insured insured1 = new Insured();
    insured1.setInsuredName("insured1");
    
    Insured insured2 = new Insured();
    insured2.setInsuredName("insured2");
    
    List list = new ArrayList();
    list.add(insured1);
    list.add(insured2);
    
    XStream xstream = new XStream(new DomDriver());
    xstream.alias("Form", List.class);
    String xml = xstream.toXML(list);
    System.out.println(xml);


In version 1.2
output:

<Form>
<sojo.sample.Insured>
<insuredName>insured1</insuredName>
</sojo.sample.Insured>
<sojo.sample.Insured>
<insuredName>insured2</insuredName>
</sojo.sample.Insured>
</Form>

In version 1.1
output:

<list>
<sojo.sample.Insured>
<insuredName>insured1</insuredName>
</sojo.sample.Insured>
<sojo.sample.Insured>
<insuredName>insured2</insuredName>
</sojo.sample.Insured>
</list>


So, i decide to give up using xstream in my next version project


That's why you should realize inheriting from third-party util classes is really
important. I don't believe you should reject for the compatible issue.
JJYAO 2007-04-13
I find another compatible problem today.

    
    Insured insured1 = new Insured();
    insured1.setInsuredName("insured1");
    
    Insured insured2 = new Insured();
    insured2.setInsuredName("insured2");
    
    List list = new ArrayList();
    list.add(insured1);
    list.add(insured2);
    
    XStream xstream = new XStream(new DomDriver());
    xstream.alias("Form", List.class);
    String xml = xstream.toXML(list);
    System.out.println(xml);


In version 1.2
output:

<Form>
<sojo.sample.Insured>
<insuredName>insured1</insuredName>
</sojo.sample.Insured>
<sojo.sample.Insured>
<insuredName>insured2</insuredName>
</sojo.sample.Insured>
</Form>

In version 1.1
output:

<list>
<sojo.sample.Insured>
<insuredName>insured1</insuredName>
</sojo.sample.Insured>
<sojo.sample.Insured>
<insuredName>insured2</insuredName>
</sojo.sample.Insured>
</list>


So, i decide to give up using xstream in my next version project
JJYAO 2007-03-30
lordhong 写道
JJYAO 写道
I don't like xstream very much. There are lots of backwards compatibility problems when you upgrade ver1.1 to ver1.2. So, xstream is not a good choice for using widely.


you are funny... we pick the latest version 1.2, no problem at all.

backward incompatible should not be the reason why we should not use xstream...



e.g

ver1.1
Class CollectionConverter(ClassMapper classMapper, java.lang.String classAttributeIdentifier) 

ver1.2
Class CollectionConverter(Mapper mapper)

ver1.1
Interface ClassMapper.lookupName(java.lang.Class type) 

ver1.2
Don't have this method

ver1.1
Interface HierarchicalStreamWriter
 void addAttribute(java.lang.String name, java.lang.String value) 
 void endNode() 
 void setValue(java.lang.String text)
 void startNode(java.lang.String name)  
 
ver1.2
Interface HierarchicalStreamWriter
 void addAttribute(java.lang.String name, java.lang.String value) 
 void close() 
 void endNode() 
 void flush() 
 void setValue(java.lang.String text) 
 void startNode(java.lang.String name) 
 HierarchicalStreamWriter underlyingWriter()  


You can find there are many differences between ver1.1 and ver1.2
and another serious problem that if your objects are loaded via hibernate and
proxied by Cglib, an error will happen. but it runs well in ver1.1.
Because ver1.2 uses cglib to enhance the process of serialization, but it has
some limits.
It is ok after i remove the reference of cglib from ver1.2
roc8633284 2007-03-30
fins 写道
xstream + xpp 的组合绝对够出色
速度快 效率高(似乎是一个意思 哈哈)
xpp3 最新版本是 xpp3-1.1.4c

XML Pull Parser是一种高速的 解析xml文件的方式
速度要比传统方式快很多(但是功能弱了些)
感兴趣的可以去google一下 "xpp"

偶像说什么我都信____追星的我
fins 2007-03-30
xstream + xpp 的组合绝对够出色
速度快 效率高(似乎是一个意思 哈哈)
xpp3 最新版本是 xpp3-1.1.4c

XML Pull Parser是一种高速的 解析xml文件的方式
速度要比传统方式快很多(但是功能弱了些)
感兴趣的可以去google一下 "xpp"
lordhong 2007-03-30
JJYAO 写道
I don't like xstream very much. There are lots of backwards compatibility problems when you upgrade ver1.1 to ver1.2. So, xstream is not a good choice for using widely.


you are funny... we pick the latest version 1.2, no problem at all.

backward incompatible should not be the reason why we should not use xstream...
JJYAO 2007-03-29
I don't like xstream very much. There are lots of backwards compatibility problems when you upgrade ver1.1 to ver1.2. So, xstream is not a good choice for using widely.
moogle 2006-11-17
目前我发现就xtream最简单,不需要生成dtd,无用配置,不需要生成辅助类,虽然功能相对其他的同类工具要简单,但是已经可以满足我的要求了. 所以才采用这个的.
together 2006-11-17
hibernate有第三方工具,可以方便的在xml/obj/db之间转换的。
zgdhj95 2006-11-17
好像转换的效率比较低~~
moogle 2006-11-16
第2个问题已经解决。
加入xpp3_min-1.1.3.4.O.jar(包含在xStream压缩包中)

XStream sm = new XStream(new DomDriver());


修改为:

XStream sm = new XStream();
moogle
搜索本博客
我的相册
E1f9db4d-86b3-46d5-8407-8009bde36b18-thumb
MSN Cartoon 生成肖像图 表情系列 13
共 14 张
最近加入圈子
存档
最新评论
评论排行榜