给定以下类:
public class Container {
private List<SomeData> list;
// public getter & setter for list
}
public class SomeData {
private String data;
// public getter & setter for data
}
当我运行以下代码时:
import java.beans.XMLEncoder;
import java.io.FileOutputStream;
SomeData someData1 = new SomeData();
SomeData someData2 = new SomeData();
someData1.setData("data1");
someData2.setData("data2");
List<SomeData> data = new ArrayList<SomeData>();
data.add(someData1);
data.add(someData2);
Container container = new Container();
container.setList(data);
FileOutputStream os = new FileOutputStream("c:\test.xml");
XMLEncoder encoder = new XMLEncoder(os);
encoder.writeObject(container);
encoder.close();
当我查看“test.xml”时,只有容器对象的数据,列表中的 SomeData 对象没有(即字符串“data1”和“data2”):
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.6.0_22" class="java.beans.XMLDecoder">
<object class="Container">
<void property="container">
<object class="java.util.ArrayList">
<void method="add">
<object class="SomeData"/>
</void>
<void method="add">
<object class="SomeData"/>
</void>
</object>
</void>
</object>
</java>
如何序列化“容器”和存储在“容器”列表中的 SomeData 对象?