使用xStream进行java object<-->xml之间的转换
官方网站:http://xstream.codehaus.org/
测试了一下,的确十分方便。
java 代码
- public static void write() {
- XStream sm = new XStream();
- mytest t = new mytest();
- t.setName("moogle");
- t.setXb("男");
- try {
- FileOutputStream ops = new FileOutputStream(new File("C:\\111.xml"));
- sm.toXML(t, ops);
- ops.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static void read() {
- XStream sm = new XStream(new DomDriver());
- try {
- FileInputStream ops = new FileInputStream(new File("C:\\111.xml"));
- mytest t = (mytest)sm.fromXML(ops);
- System.out.println(t.getName());
- ops.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
生成的XML文件内容:
xml 代码
- <mytest>
- <name>asd</name>
- <xb>男</xb>
- </mytest>
问题:
1.生成的xml文档没有
2.如果生成的文档中含有中文,比如上文代码中setXb("男")
在读取的时候会报
[Fatal Error] :4:7: Invalid byte 2 of 2-byte UTF-8 sequence.
请指教。
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
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)
Version: xstream 1.2.1 To make comments on XStream.java from line 635 to 640, then re-compile :P
支持xStream,一直用它!好使!!
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.
JJYAO,how to remove the reference of cglib?
fire使用的却是xmlbean
已经习惯了用JAXB了!各方面都还满意
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.
I find another compatible problem today.
In version 1.2 output:
In version 1.1 output:
So, i decide to give up using xstream in my next version project
e.g
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 :)
偶像说什么我都信____追星的我
xstream + xpp 的组合绝对够出色 速度快 效率高(似乎是一个意思 哈哈) xpp3 最新版本是 xpp3-1.1.4c
XML Pull Parser是一种高速的 解析xml文件的方式 速度要比传统方式快很多(但是功能弱了些) 感兴趣的可以去google一下 "xpp"
backward incompatible should not be the reason why we should not use xstream...
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.
目前我发现就xtream最简单,不需要生成dtd,无用配置,不需要生成辅助类,虽然功能相对其他的同类工具要简单,但是已经可以满足我的要求了. 所以才采用这个的.
hibernate有第三方工具,可以方便的在xml/obj/db之间转换的。
好像转换的效率比较低~~
第2个问题已经解决。 加入xpp3_min-1.1.3.4.O.jar(包含在xStream压缩包中) 将
修改为: