为什么 XmlEncoder 不将类的成员插入到 xml 文件中?

发布于 2024-12-31 22:10:28 字数 2724 浏览 3 评论 0原文

我正在尝试将一些数据放入 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月亮邮递员 2025-01-07 22:10:29

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 or isAbc/setAbc for boolean properties, etc.) My guess is that the only property available like that in your class is Name, 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文