I have two classes, Package and ModelRefObj. Package contains two sets of ModelRefObj. I'm using Simple framework to parse their instances from XML, so I've created some JUnit tests. I'm able to parse ModelRefObj XML, but I'm getting the following exception when trying to parse a Package:

org.simpleframework.xml.core.ValueRequiredException: Empty value for @org.simpleframework.xml.Text(empty=, data=false, required=true) on field 'value' private java.lang.String cz.semanta.coc.domain.cognos.ModelRefObj.value in class cz.semanta.coc.domain.cognos.ModelRefObj at line 1
    at org.simpleframework.xml.core.Composite.readInstance(Composite.java:580)
    at org.simpleframework.xml.core.Composite.readText(Composite.java:467)
    at org.simpleframework.xml.core.Composite.access$200(Composite.java:59)
    at org.simpleframework.xml.core.Composite$Builder.read(Composite.java:1381)
    ...

Here is the XML I'm trying to parse:

<package> 
    <name>GO Sales (nalysis)</name>
    <visible>
        <refObj>[go_sales]</refObj>
        <refObj>[Filters and calculations].[Returns]</refObj>
    </visible>
    <hidden>
        <refObj>[gosales].[BRANCH].[BRANCH_CODE]</refObj>
        <refObj>[gosales].[BRANCH].[ADDRESS1]</refObj>
        <refObj>[gosales].[BRANCH].[CITY]</refObj>
    </hidden>
</package>

Here are my annotated classes:

@Root(name = "package")
public class Package {

    @Element
    private String name;

    @ElementList(name = "visible", entry = "refObj", type = ModelRefObj.class)
    private Set<ModelRefObj> visibleRefObjs;

    @ElementList(name = "hidden", entry = "refObj", type = ModelRefObj.class)
    private Set<ModelRefObj> hiddenRefObjs;

    Package() { }

    ...
}

@Root(name = "refObj")
public class ModelRefObj {

    @Text
    private String value;

    ModelRefObj() { }

    public ModelRefObj(String value) {
        this.value = value;
    }

    ...
}
有帮助吗?

解决方案

I have implemented the classes you have and used the example xml you provided.

I created a main function to test

public static void main(String args[]) throws Exception {
    Serializer serializer = new Persister(new Format("<?xml version=\"1.0\" encoding= \"UTF-8\" ?>"));
    File source = new File("sample.xml");
    Package p = serializer.read(Package.class, source);

    System.out.println(p.name);
}

The output is

GO Sales (nalysis)

Inspecting the object p in debug mode shows it has the two Sets with two and three elements. Your code works fine for me.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top