`
moogle
  • 浏览: 109005 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

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

    博客分类:
  • Java
阅读更多
官方网站: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.

请指教。

分享到:
评论
19 楼 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
18 楼 david3d 2007-07-12  
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)


17 楼 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});
//        }
16 楼 wtusmchen 2007-07-12  
支持xStream,一直用它!好使!!
15 楼 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.
14 楼 teligen 2007-06-27  
JJYAO,how to remove the reference of cglib?
13 楼 oksonic 2007-04-16  
fire使用的却是xmlbean
12 楼 生命火花 2007-04-16  
已经习惯了用JAXB了!各方面都还满意
11 楼 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.
10 楼 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
9 楼 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
8 楼 roc8633284 2007-03-30  
fins 写道
xstream + xpp 的组合绝对够出色
速度快 效率高(似乎是一个意思 哈哈)
xpp3 最新版本是  xpp3-1.1.4c

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

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

XML Pull Parser是一种高速的 解析xml文件的方式
速度要比传统方式快很多(但是功能弱了些)
感兴趣的可以去google一下 "xpp"
6 楼 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...
5 楼 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.
4 楼 moogle 2006-11-17  
目前我发现就xtream最简单,不需要生成dtd,无用配置,不需要生成辅助类,虽然功能相对其他的同类工具要简单,但是已经可以满足我的要求了. 所以才采用这个的.
3 楼 together 2006-11-17  
hibernate有第三方工具,可以方便的在xml/obj/db之间转换的。
2 楼 zgdhj95 2006-11-17  
好像转换的效率比较低~~

1 楼 moogle 2006-11-16  
第2个问题已经解决。
加入xpp3_min-1.1.3.4.O.jar(包含在xStream压缩包中)

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


修改为:

XStream sm = new XStream();

相关推荐

    使用XStream, java Object 与xml之间的转换,以及写入SD卡

    总之,XStream提供了一种直观且易于使用的机制来处理Java对象与XML之间的转换。结合Android的文件操作,我们可以方便地将数据序列化后存储到外部存储中,以便于后续读取和使用。通过熟练掌握XStream,开发者可以更...

    使用XStream是实现XML与Java对象的转换(5)--Object Stream

    在Java开发中,数据序列...总之,XStream是一个强大且易于使用的工具,可以帮助开发者轻松地在XML和Java对象之间进行转换。通过理解其工作原理和提供的功能,我们可以更有效地在Java项目中利用XML进行数据存储和交换。

    xml解析及使用XStream实现javaBean与xml之间的转换

    在`xmlAnalysis`文件夹中,可能包含了一个简单的Java程序,演示了如何使用DOM、SAX、StAX和XStream解析XML文件,并展示了XStream如何在JavaBean与XML之间进行转换。你可以运行这些代码,观察输出结果,以加深理解。...

    使用XStream是实现XML与Java对象的转换(4)--转换器

    总之,XStream是一个强大的工具,可以帮助开发者轻松地在XML和Java对象之间进行转换。通过自定义转换器,我们可以精确控制序列化和反序列化的过程,满足各种复杂需求。在实际开发中,熟练掌握XStream的使用,能够...

    XStream在JavaBean与XML/JSON 之间相互转换

    **XStream:JavaBean与XML/JSON之间的转换大师** XStream是一个开源库,它为Java对象提供了简单且直观的XML序列化和反序列化的解决方案。它不仅能够将Java对象转换成XML,反之亦然,还能支持JSON格式的转换。这个...

    xstream将xml文档转换成json对象

    // 使用Jackson的XML解析库将XML转换为Java对象 ObjectMapper xmlMapper = new XmlMapper(); Person deserializedPerson = xmlMapper.readValue(xml, Person.class); // 使用Gson将Java对象转换为JSON Gson gson = ...

    XStream:Object与Xml字符串 互相转换

    总的来说,XStream是一个功能丰富的库,它使得Java对象与XML之间的转换变得简单易行。通过理解和掌握XStream的用法,开发者可以更高效地处理数据序列化需求,无论是在存储、传输还是解析XML文档的场景下。

    xStream转换xml和json源码

    总的来说,xStream是Java开发中处理XML和JSON的强大工具,它提供了简单易用的API来实现对象与这两种数据格式之间的转换。通过理解和掌握xStream的使用,开发者可以更高效地处理数据序列化和反序列化任务。

    转载 xStream完美转换XML、JSON

    标题 "xStream完美转换XML、JSON" 指的是使用xStream库在Java中进行XML与JSON数据格式之间的转换。xStream是一个强大的库,它提供了一种简单的方式来序列化和反序列化Java对象到XML,反之亦然。在这个场景中,它同样...

    XStream组件进行Object与XML互转用法

    总之,XStream 提供了一个强大而灵活的工具,使得Java对象和XML之间的转换变得简单,适合在各种场景下使用。在实际开发中,根据项目的具体需求,合理配置XStream可以极大地提升代码的可维护性和可读性。

    基于java的开发源码-转换xml.zip

    总的来说,这个“基于java的开发源码-转换xml.zip”可能包含了如何使用XStream进行XML和Java对象之间的转换的实际代码示例,对于学习或工作中处理XML数据的Java开发者来说,这是一个非常有价值的资源。通过深入理解...

    使用 XStream 把 Java 对象序列化为 XML

    它的核心功能是能够将任何Java对象转换为XML,然后再从XML转换回来,而无需编写大量的代码。这使得数据交换、持久化或者网络传输变得非常便捷。 **序列化过程** 1. **配置XStream实例**:首先,我们需要创建一个...

    XStream实现Object与XML转换解决方案

    标题中的“XStream实现Object与XML转换解决方案”指的是一个Java库——XStream,它提供了一种简单的方法来序列化和反序列化Java对象到XML,反之亦然。这个库广泛用于将程序数据保存到XML文件或者从XML数据恢复对象,...

    使用XStream是实现XML与Java对象的转换(6)--持久化

    2. **对象到XML转换**:通过调用`toXML()`方法,我们可以将任何Java对象转换为XML字符串。这个过程会递归地处理对象的所有字段,除非它们被标记为忽略。 3. **XML到对象转换**:使用`fromXML()`方法,我们可以从XML...

    XStream使用方法总结-对象-转换.txt

    XStream是一款强大的Java库,主要用于实现Java对象与XML文档之间的相互转换。本文将详细介绍XStream的基本用法、配置选项以及如何利用它来进行对象到XML的序列化和反序列化操作。 #### 一、XStream简介 XStream是...

    使用XStream操作xml教程

    总结,XStream是一个强大且易于使用的库,能够简化Java对象与XML之间的转换。通过了解其基本用法和自定义功能,开发者可以在项目中有效地利用XML进行数据交换和存储。在实际应用中,根据项目需求进行适当的配置和...

    将对象生成xml的jar包,xstream-1.4.2,xmlpull-1.1.3.1,xpp3_min-1.1.4c

    本主题涉及三个关键库:XStream 1.4.2,XMLPull 1.1.3.1和XPP3_min 1.1.4c,它们在对象到XML转换中扮演着重要角色。 **XStream 1.4.2** 是一个强大的Java库,它能够将Java对象序列化为XML,并从XML反序列化回Java...

    XStream的Date转换 @XStreamConverter DateConverter

    它们可能会创建一些包含Date对象的Java类,然后使用XStream实例进行序列化和反序列化操作,确保日期的转换符合预期。测试代码可能如下所示: ```java import com.thoughtworks.xstream.XStream; import ...

    XStream4JavaDemo演示源码

    通过这个示例,我们可以了解到XStream如何简化Java对象与XML之间的转换,以及如何自定义转换规则以满足特定需求。无论是进行数据持久化,还是进行网络数据交换,XStream都是一个非常实用的工具。在深入研究源码时,...

Global site tag (gtag.js) - Google Analytics