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.
You need to make your data members private in Class B and it will work fine. try this code.
private String s;
private HashSet<String> h = new HashSet<String>();
You need to follow java conventions in defining all your classes. XMLEncode will use getter/setter methods to properly convert objects into xml.