从 java 1.6.x 迁移到 1.7.x 后 XMLEncoder 的行为发生变化
我已经在 Java 7 下重新编译了一些代码并开始测试。很快我就注意到用于将对象序列化为 xml 的机制停止工作。幸运的是,此序列化用于临时会话存储目的,因此我可以删除所有旧的序列化 XML 并使用 xstream 从头开始XMLEncoder 的。
我很好奇我做错了什么或者 Java7 附带的 XMLEncoder 发生了什么变化。
B.java:
import java.util.*;
public class B{
public String s;
public void setS(String s){
this.s = s;
}
public String getS(){
return this.s;
}
public HashSet<String> h = new HashSet<String>();
public void setH(HashSet<String> h){
this.h = h;
}
public HashSet<String> getH(){
return this.h;
}
}
A.java:
import java.util.*;
import java.io.*;
import java.beans.*;
class A{
public A(){
B tmp = new B();
tmp.setS("abc");
HashSet<String>h = new HashSet<String>(Arrays.asList("a", "c"));
tmp.setH(h);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
XMLEncoder e = new XMLEncoder( new BufferedOutputStream(bos) );
e.writeObject( tmp );
e.close();
System.out.println(bos.toString());
}
public static void main(String []arg){
new A();
}
}
在 java 1.6.x 下运行 A 给出:
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.6.0_25" class="java.beans.XMLDecoder">
<object class="B">
<void property="h">
<void method="add">
<string>b</string>
</void>
<void method="add">
<string>a</string>
</void>
</void>
<void property="s">
<string>abc</string>
</void>
</object>
</java>
在 java 1.7.0_01 下运行 A 给出:
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.7.0_01" class="java.beans.XMLDecoder">
<object class="B" id="B0">
<void class="B" method="getField">
<string>s</string>
<void method="set">
<object idref="B0"/>
<string>abc</string>
</void>
</void>
</object>
</java>
如您所见,输出不包含 HashSet h 字段的任何痕迹。
我已经做了一些谷歌搜索,但到目前为止,我能找到的唯一类似的情况是 这篇文章,没有别的。
预先感谢您的提示。
I've recompiled some of my code under Java 7 and started testing. Quite soon I've noticed that mechanism I used for serialization of objects to xml stopped working. Luckily this serialization is for temporary session storage purpose so I could remove all old serialized XMLs and start from scratch using xstream instead of XMLEncoder.
I'm curious what have I done wrong or what changed in the XMLEncoder shipped with Java7.
B.java:
import java.util.*;
public class B{
public String s;
public void setS(String s){
this.s = s;
}
public String getS(){
return this.s;
}
public HashSet<String> h = new HashSet<String>();
public void setH(HashSet<String> h){
this.h = h;
}
public HashSet<String> getH(){
return this.h;
}
}
A.java:
import java.util.*;
import java.io.*;
import java.beans.*;
class A{
public A(){
B tmp = new B();
tmp.setS("abc");
HashSet<String>h = new HashSet<String>(Arrays.asList("a", "c"));
tmp.setH(h);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
XMLEncoder e = new XMLEncoder( new BufferedOutputStream(bos) );
e.writeObject( tmp );
e.close();
System.out.println(bos.toString());
}
public static void main(String []arg){
new A();
}
}
Running A under java 1.6.x gives me:
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.6.0_25" class="java.beans.XMLDecoder">
<object class="B">
<void property="h">
<void method="add">
<string>b</string>
</void>
<void method="add">
<string>a</string>
</void>
</void>
<void property="s">
<string>abc</string>
</void>
</object>
</java>
Running A under java 1.7.0_01 gives me:
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.7.0_01" class="java.beans.XMLDecoder">
<object class="B" id="B0">
<void class="B" method="getField">
<string>s</string>
<void method="set">
<object idref="B0"/>
<string>abc</string>
</void>
</void>
</object>
</java>
As you can see the output does not contain any trace of the HashSet h field.
I've done some googling but so far the only similar case that I could find is this post, nothing else.
Thanks in advance for your hints.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将 B 类中的数据成员设置为私有,这样就可以正常工作。试试这个代码。
您需要遵循 java 约定来定义所有类。 XMLEncode 将使用 getter/setter 方法将对象正确转换为 xml。
You need to make your data members private in Class B and it will work fine. try this code.
You need to follow java conventions in defining all your classes. XMLEncode will use getter/setter methods to properly convert objects into xml.