java - 如何使用 SimpleXML 跨序列化保留结构?

标签 java xml-parsing xml-serialization xml-deserialization simple-framework

我是 SimpleXML 的新手,我认为它声称能够做到这一点,所以我一定做错了什么。也许你可以帮我。

问题是,如果我有一个父对象,其中有两个指向同一个子对象的链接,那么当反序列化父对象时,我现在有两个指向相同但不同的对象(内存中不同位置)的链接。这破坏了结构。

这就是我正在做的事情:

主题.java:

@Root (name = "Topic")
public class Topic {

    @Attribute (name = "name")
    String name = null;

    @Element (name = "id")
    int id = -1;

    @ElementList (name = "sparks")
    ArrayList<Spark> sparks = null;

    public Topic(
            @Attribute (name = "name") String inName,
            @Element (name = "id") int inID,
            @ElementList (name = "sparks") ArrayList<Spark> inSparks) {
        name = new String(inName);
        id = inID;
        if (inSparks == null) {
            sparks = new ArrayList<>(50);
        } else {
            sparks = inSparks;
        }
    }
[...]

Spark.java

@Root (name = "Spark")
public class Spark {

    @Element (name = "text", required = false)
    String text = null;

    @Element (name = "dateCompleted", required = false)
    Date dateCompleted = null;

    @Element (name = "rejected")
    boolean rejected = false;

    @Element (name = "delayedUntil", required = false)
    Date delayedUntil = null;

    @Attribute (name = "id")
    int id = -1; 

    @Element (name = "packName", required = false)
    String packName = null;

    public Spark(
            @Element (name = "text") String inText,
            @Attribute (name = "id") int inID,
            @Element (name = "packName") String inPackName) {
        text = new String(inText);
        id = inID;
        packName = new String(inPackName);
    }

    [...]

问题演示:

    Serializer serializer = new Persister();
    File saveFile = new File(this.getFilesDir(), "test.xml");

    Topic testTopic = new Topic("TestTopic", 1);
    Spark newSpark = new Spark ("This is the sample text.", 1, "PretendPack");
    testTopic.sparks.add(newSpark);
    testTopic.sparks.add(newSpark);

    try {
        serializer.write(testTopic, saveFile);
    } catch (Exception e) {
        e.printStackTrace();
    }

    Topic newTopic = null;
    try {
        newTopic = serializer.read(Topic.class, saveFile);
    } catch (Exception e) {
        Log.e("IntimacyToolbox", "Was unable to deserialize from the savedData.");
        e.printStackTrace();
    }
    Spark spark1 = newTopic.sparks.get(0);
    Spark spark2 = newTopic.sparks.get(1);
    if (spark1 == spark2) {
        Log.d("IntimacyToolbox", "Good: sparks are the same.");
    } else {
        Log.d("IntimacyToolbox", "Bad: sparks are different objects.");
    }

运行这段代码后,newTopic.sparks ArrayList中的两个spark是不同的对象。有没有办法让它们反序列化为同一个对象?我来自 iOS,那里的系统运行得非常神奇;看来 Android 上应该有类似的东西;但也许 SimpleXML 不是。

提前致谢!

最佳答案

是的,使用 CycleStrategy。

关于java - 如何使用 SimpleXML 跨序列化保留结构?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32016137/

相关文章:

java - Spring + Velocity 电子邮件模板未在邮件正文上呈现 Html

java - 在 fragment 中打开数据库

java - Java中解析XML的高效方法

java - 如何抑制 xml :base attribute that the java. xml Transformer 正在添加到导入的外部系统实体?

c# - 在 WCF 中创建自定义 Nullable 类实现

java - TAI 的 isTargetInterceptor 方法没有被调用

java - 在 Java3D 中选择并移动多个形状

Perl 使用 XML 路径上下文提取数据

c# - 如何将 XML 文件反序列化为具有只读属性的类?