2006-11-16
使用xStream进行java object<-->xml之间的转换
关键字: xStream官方网站: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.
请指教。
评论
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
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.
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.
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.
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
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.
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
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.
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"
速度快 效率高(似乎是一个意思 哈哈)
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"
速度快 效率高(似乎是一个意思 哈哈)
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.
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压缩包中)
将
修改为:
加入xpp3_min-1.1.3.4.O.jar(包含在xStream压缩包中)
将
XStream sm = new XStream(new DomDriver());
修改为:
XStream sm = new XStream();
- 浏览: 33739 次
- 性别:


- 详细资料
搜索本博客
我的相册
MSN Cartoon 生成肖像图 表情系列 13
共 14 张
共 14 张
最新评论
-
PTViewer 全景显示
请问全景显示到底是怎么回事呢? 是不是先用软件做好全景图,然后插入网页中显示;我 ...
-- by wayer -
[转]UrlRewrite Filter
请问 为什么 我的 项目加了 urlrewrite 好像是没有这个标签 ...
-- by wangjian3q -
配置Spring 支持 Web ses ...
<context-param> <param-nam ...
-- by MrLee23 -
配置Spring 支持 Web ses ...
给你代码整理下,你的看的有点别扭` <context-param> ...
-- by MrLee23 -
使用xStream进行java obj ...
Thanks. JJYAO. I have replied you severa ...
-- by david3d






评论排行榜