为什么 XmlEncoder 不将类的成员插入到 xml 文件中?
我正在尝试将一些数据放入 XML 文件中。这是代码:
@Test
public void TestProximities() {
// Create a proximity
ISpaceComponent solarSystem = Init.createSolarSystemProximities();
solarSystem.runTtest();
Path filePath = Paths.get("c:\\temp\\junk.bin");
File xmlFile = new File("c:\\temp\\");
boolean success = xmlFile.mkdirs();
if (!success && ! xmlFile.exists() ) {
// Directory creation failed
System.out.println("Failed to create a file: " + filePath);
}
try {
XMLEncoder fileXml = new XMLEncoder( new BufferedOutputStream( new FileOutputStream("c:\\temp\\junk.xml") ) );
fileXml.writeObject(solarSystem);
fileXml.flush();
fileXml.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
Init.createSolarSystemProximities():
public static ISpaceComposite createSolarSystemProximities() {
// Proximities
ISpaceComposite solarSystem = new SpaceComposite("The Solar System");
solarSystem.addComponent(new Star("Sol"));
ISpaceComposite MercuryProximity = new SpaceComposite("Mercury proximity");
MercuryProximity.addComponent(new Planet("Mercury"));
solarSystem.addComponent(MercuryProximity);
ISpaceComposite VenusProximity = new SpaceComposite("Venus proximity");
VenusProximity.addComponent(new Planet("Venus"));
solarSystem.addComponent(VenusProximity);
ISpaceComposite EarthProximity = new SpaceComposite("Earth proximity");
EarthProximity.addComponent(new Planet("The Earth"));
EarthProximity.addComponent(new Moon("The Moon"));
solarSystem.addComponent(EarthProximity);
ISpaceComposite MarsProximity = new SpaceComposite("Mars proximity");
MarsProximity.addComponent(new Planet("Mars"));
MarsProximity.addComponent(new Moon("Phobos"));
MarsProximity.addComponent(new Moon("Deimos"));
solarSystem.addComponent(MarsProximity);
return solarSystem;
}
空间复合类看起来像这样:
public class SpaceComposite extends SpaceComponent implements ISpaceComposite {
public static final long serialVersionUID = Data.serialVersionUID;
private ArrayList<ISpaceComponent> _components;
private int _currentMoonIndex;
}
另外“SpaceComposite”具有默认的无参数构造函数。但插入到文件中的唯一内容是它的名称,它派生自“SpaceComponent”类
public abstract class SpaceComponent implements ISpaceComponent, Serializable{
private String _title;
public static final long serialVersionUID = Data.serialVersionUID;
// Default constructor
public SpaceComponent(){
_title = "";
}
}
我该如何解决这个问题?
I'm trying to put into XML-file some data. Here is the code:
@Test
public void TestProximities() {
// Create a proximity
ISpaceComponent solarSystem = Init.createSolarSystemProximities();
solarSystem.runTtest();
Path filePath = Paths.get("c:\\temp\\junk.bin");
File xmlFile = new File("c:\\temp\\");
boolean success = xmlFile.mkdirs();
if (!success && ! xmlFile.exists() ) {
// Directory creation failed
System.out.println("Failed to create a file: " + filePath);
}
try {
XMLEncoder fileXml = new XMLEncoder( new BufferedOutputStream( new FileOutputStream("c:\\temp\\junk.xml") ) );
fileXml.writeObject(solarSystem);
fileXml.flush();
fileXml.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}
Init.createSolarSystemProximities():
public static ISpaceComposite createSolarSystemProximities() {
// Proximities
ISpaceComposite solarSystem = new SpaceComposite("The Solar System");
solarSystem.addComponent(new Star("Sol"));
ISpaceComposite MercuryProximity = new SpaceComposite("Mercury proximity");
MercuryProximity.addComponent(new Planet("Mercury"));
solarSystem.addComponent(MercuryProximity);
ISpaceComposite VenusProximity = new SpaceComposite("Venus proximity");
VenusProximity.addComponent(new Planet("Venus"));
solarSystem.addComponent(VenusProximity);
ISpaceComposite EarthProximity = new SpaceComposite("Earth proximity");
EarthProximity.addComponent(new Planet("The Earth"));
EarthProximity.addComponent(new Moon("The Moon"));
solarSystem.addComponent(EarthProximity);
ISpaceComposite MarsProximity = new SpaceComposite("Mars proximity");
MarsProximity.addComponent(new Planet("Mars"));
MarsProximity.addComponent(new Moon("Phobos"));
MarsProximity.addComponent(new Moon("Deimos"));
solarSystem.addComponent(MarsProximity);
return solarSystem;
}
And the space composite class is looking like this:
public class SpaceComposite extends SpaceComponent implements ISpaceComposite {
public static final long serialVersionUID = Data.serialVersionUID;
private ArrayList<ISpaceComponent> _components;
private int _currentMoonIndex;
}
Also 'SpaceComposite' has default prameterless constructor. But the only thing, which is inserted into a file, is its name, derrived from the 'SpaceComponent' class
public abstract class SpaceComponent implements ISpaceComponent, Serializable{
private String _title;
public static final long serialVersionUID = Data.serialVersionUID;
// Default constructor
public SpaceComponent(){
_title = "";
}
}
How can I solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
XmlEncoder
使用 JavaBean 约定捕获属性。它的工作原理是克隆对象图并记录创建克隆所需的步骤。您希望在不进行任何自定义的情况下与 XmlEncoder 一起使用的类需要遵循 JavaBeans 约定(属性的getXYZ/setXYZ
或布尔属性的isAbc/setAbc
等)。猜测是类中唯一可用的属性是Name
,因此它是唯一被序列化的属性。链接中的文章还解释了如何通过提供自己的持久性委托来自定义流程,但我会首先尝试添加 getter/setter,看看这是否足以使其工作。
XmlEncoder
captures properties using JavaBean conventions. It works by cloning the object graph and recording the steps that were necessary to create the clone. A class that you would like to use with XmlEncoder without any customizations needs to follow JavaBeans conventions (getXYZ/setXYZ
for properties orisAbc/setAbc
for boolean properties, etc.) My guess is that the only property available like that in your class isName
, hence it's the only one being serialized.The article at the link also explains how you can customize the process by providing your own persistence delegate, but I would try adding getters/setters first to see if that is enough to make it work.