ITeye博客

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

moogle · 2006-11-16 23:28 · Java · Markdown

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

生成的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.

请指教。

评论(19)

  1. #19 楼 · david3d · 2007-07-16 13:01

    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

  2. #18 楼 · david3d · 2007-07-12 16:06

    I've tied the re-compile but still have problem. Maybe there's some other root cause for my case.

    Anyway, Thanks for your reply.

    Exception in thread "main" java.lang.ClassCastException: org.codehaus.jettison.json.JSONArray at org.codehaus.jettison.mapped.MappedXMLStreamWriter.writeAttribute(MappedXMLStreamWriter.java:93) at org.codehaus.jettison.mapped.MappedXMLStreamWriter.writeAttribute(MappedXMLStreamWriter.java:130) at org.codehaus.jettison.mapped.MappedXMLStreamWriter.writeAttribute(MappedXMLStreamWriter.java:134) at com.thoughtworks.xstream.io.xml.StaxWriter.addAttribute(StaxWriter.java:86) at com.thoughtworks.xstream.io.WriterWrapper.addAttribute(WriterWrapper.java:30) at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:43) at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:50) at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.writeItem(AbstractCollectionConverter.java:52) at com.thoughtworks.xstream.converters.collections.MapConverter.marshal(MapConverter.java:46) at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:55) at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:50) at com.thoughtworks.xstream.annotations.AnnotationReflectionConverter.marshallField(AnnotationReflectionConverter.java:46) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.writeField(AbstractReflectionConverter.java:112) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.visit(AbstractReflectionConverter.java:88) at com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider.visitSerializableFields(PureJavaReflectionProvider.java:117) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doMarshal(AbstractReflectionConverter.java:73) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:43) at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:55) at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:50) at com.thoughtworks.xstream.annotations.AnnotationReflectionConverter.marshallField(AnnotationReflectionConverter.java:46) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.writeField(AbstractReflectionConverter.java:112) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.visit(AbstractReflectionConverter.java:88) at com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider.visitSerializableFields(PureJavaReflectionProvider.java:117) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doMarshal(AbstractReflectionConverter.java:73) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:43) at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:55) at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:50) at com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter.writeItem(AbstractCollectionConverter.java:52) at com.thoughtworks.xstream.converters.collections.CollectionConverter.marshal(CollectionConverter.java:44) at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:55) at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:50) at com.thoughtworks.xstream.annotations.AnnotationReflectionConverter.marshallField(AnnotationReflectionConverter.java:46) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.writeField(AbstractReflectionConverter.java:112) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.visit(AbstractReflectionConverter.java:88) at com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider.visitSerializableFields(PureJavaReflectionProvider.java:117) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doMarshal(AbstractReflectionConverter.java:73) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:43) at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:55) at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:50) at com.thoughtworks.xstream.annotations.AnnotationReflectionConverter.marshallField(AnnotationReflectionConverter.java:46) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.writeField(AbstractReflectionConverter.java:112) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.visit(AbstractReflectionConverter.java:88) at com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider.visitSerializableFields(PureJavaReflectionProvider.java:117) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doMarshal(AbstractReflectionConverter.java:73) at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:43) at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:55) at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:50) at com.thoughtworks.xstream.core.TreeMarshaller.start(TreeMarshaller.java:73) at com.thoughtworks.xstream.core.ReferenceByXPathMarshallingStrategy.marshal(ReferenceByXPathMarshallingStrategy.java:34) at com.thoughtworks.xstream.XStream.marshal(XStream.java:765) at com.thoughtworks.xstream.XStream.marshal(XStream.java:754) at com.thoughtworks.xstream.XStream.toXML(XStream.java:735) at com.thoughtworks.xstream.XStream.toXML(XStream.java:725) at com.kss.immg.tec.ProdInfoActionTest.testDetachedObjectWithSetPropertyToJson(ProdInfoActionTest.java:103) at com.kss.immg.tec.ProdInfoActionTest.main(ProdInfoActionTest.java:22)

  3. #17 楼 · JJYAO · 2007-07-12 09:59

    david3d writes:

    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 :P

    //        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});
    //        }
    
  4. #16 楼 · wtusmchen · 2007-07-12 09:32

    支持xStream,一直用它!好使!!

  5. #15 楼 · david3d · 2007-07-11 23:56

    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.

  6. #14 楼 · teligen · 2007-06-27 10:59

    JJYAO,how to remove the reference of cglib?

  7. #13 楼 · oksonic · 2007-04-16 15:20

    fire使用的却是xmlbean

  8. #12 楼 · 生命火花 · 2007-04-16 15:09

    已经习惯了用JAXB了!各方面都还满意

  9. #11 楼 · JAVA_ED · 2007-04-16 15:06

    JJYAO writes:

    I find another compatible problem today.

    [code]
    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);
    

    [/code]

    In version 1.2 output:

    In version 1.1 output:

    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.

  10. #10 楼 · JJYAO · 2007-04-13 16:32

    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:

    In version 1.1 output:

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

  11. #9 楼 · JJYAO · 2007-03-30 11:16

    lordhong writes:

    [quote=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.[/quote]

    1. 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 :)

  12. #8 楼 · roc8633284 · 2007-03-30 10:14

    fins writes:

    xstream + xpp 的组合绝对够出色 速度快 效率高(似乎是一个意思 哈哈) xpp3 最新版本是 xpp3-1.1.4c

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

    偶像说什么我都信____追星的我

  13. #7 楼 · fins · 2007-03-30 09:27

    xstream + xpp 的组合绝对够出色 速度快 效率高(似乎是一个意思 哈哈) xpp3 最新版本是 xpp3-1.1.4c

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

  14. #6 楼 · lordhong · 2007-03-30 05:09

    JJYAO writes:

    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.

    1. 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...

  15. #5 楼 · JJYAO · 2007-03-29 23:20

    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.

  16. #4 楼 · moogle · 2006-11-17 09:01

    目前我发现就xtream最简单,不需要生成dtd,无用配置,不需要生成辅助类,虽然功能相对其他的同类工具要简单,但是已经可以满足我的要求了. 所以才采用这个的.

  17. #3 楼 · together · 2006-11-17 08:44

    hibernate有第三方工具,可以方便的在xml/obj/db之间转换的。

  18. #2 楼 · zgdhj95 · 2006-11-17 08:29

    好像转换的效率比较低~~

  19. #1 楼 · moogle · 2006-11-16 23:48

    第2个问题已经解决。 加入xpp3_min-1.1.3.4.O.jar(包含在xStream压缩包中) 将

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

    修改为:

    XStream sm = new XStream();
    

评论功能维护中,当前仅展示历史评论。