在Simple 2.5.3(Java)中反序列化重复的XML元素

|| 假设给出了以下XML:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<ResC>
    <Err text=\"Error text 1\"/>
    <ConRes>
        <Err text=\"Error text 2\"/>
        <ConList>
            <Err text=\"Error text 3\"/>
            <Con>
                <Err text=\"Error text 4\"/>
            </Con>
        </ConList>
    </ConRes>
</ResC>
如您所见,“ 1”元素可能会出现在XML的每个级别上。 使用简单我想反序列化此XML。因此,我创建了以下类:
@Element(required=false)
public class Err {
    @Attribute
    private String text;

    public void setText(String text) { this.text = text; }

    public String getText() { return text; }
}
但是,如何注释have3ѭ,
<ConRes>
<ConList>
<Con>
的类?我真的必须在可能出现的每个类中声明一个类型为
<Err>
的属性吗?这似乎有很多开销。如果是这样,那么我将必须检查每个对象是否包含错误。 有没有更好更好的方法? :-) 谢谢, 罗伯特     
已邀请:
        要记住的重要一点是,简单XML应该能够遵循您可以使用类在逻辑上生成的任何结构。因此,您可以只创建一个使用错误接口的BaseClass并应用Decorator模式,以便将所有这些传递给具体的错误类,而无需任何实现对象知道它们给出了什么。 那可能没有任何意义。我如何向您展示...好吧...我只是走开了,并完全按照我的想法实现了,这里是结果(完整的代码链接): 主文件:
package com.massaiolir.simple.iface;

import java.io.File;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

public class Main {
    public static void main(String[] args) throws Exception {
        Serializer serial = new Persister();
        ResC resc = serial.read(ResC.class, new File(\"data/testdata.xml\"));

        System.out.println(\" == Printing out all of the error text. == \");
        System.out.println(resc.getErrorText());
        System.out.println(resc.conRes.getErrorText());
        System.out.println(resc.conRes.conList.getErrorText());
        for (Con con : resc.conRes.conList.cons) {
            System.out.println(con.getErrorText());
        }
        System.out.println(\" == Finished printing out all of the error text. == \");
    }
}
它只是运行简单并显示结果。 BaseObject.java类:
package com.massaiolir.simple.iface;

import org.simpleframework.xml.Element;

public class BaseObject implements Error {
    @Element(name = \"Err\", required = false, type = ConcreteError.class)
    private Error err;

    @Override
    public String getErrorText() {
        return err.getErrorText();
    }

    @Override
    public void setErrorText(String errorText) {
        err.setErrorText(errorText);
    }
}
这是所有想要\'Err \'都应该扩展的类。 错误界面:
package com.massaiolir.simple.iface;

public interface Error {
    void setErrorText(String errorText);

    String getErrorText();
}
ConcreteError类:
package com.massaiolir.simple.iface;

import org.simpleframework.xml.Attribute;

public class ConcreteError implements Error {
    @Attribute
    private String text;

    @Override
    public String getErrorText() {
        return text;
    }

    @Override
    public void setErrorText(String errorText) {
        this.text = errorText;
    }

}
实际的实现类在此之后。您会发现它们相当琐碎,因为上面的类正在处理实际的工作。 Con类:
package com.massaiolir.simple.iface;

public class Con extends BaseObject {

}
ConList类:
package com.massaiolir.simple.iface;

import java.util.ArrayList;

import org.simpleframework.xml.ElementList;

public class ConList extends BaseObject {
    @ElementList(entry = \"Con\", inline = true)
    public ArrayList<Con> cons;
}
ConRes类:
package com.massaiolir.simple.iface;

import org.simpleframework.xml.Element;

public class ConRes extends BaseObject {
    @Element(name = \"ConList\")
    public ConList conList;
}
ResC类:
package com.massaiolir.simple.iface;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

@Root
public class ResC extends BaseObject {
    @Element(name = \"ConRes\")
    public ConRes conRes;
}
这就是全部。很简单的权利。我能够在十分钟内将所有内容全部消除。实际上,编写此响应所花的时间要比编写给您的代码所花的时间长。如果您对我刚刚编写的代码一无所知,请告诉我。我希望这可以帮助您了解如何做这样的事情。     

要回复问题请先登录注册