我有两个类, Package 和 ModelRefObj 。包中包含两组 ModelRefObj 。 我使用Simple框架从XML解析他们的实例,所以我创建了一些JUnit测试。我能够解析 ModelRefObj XML,但在尝试解析包时我收到以下异常:
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)
...
以下是我试图解析的XML:
<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>
以下是我的注释类:
@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;
}
...
}
答案 0 :(得分:1)
我已经实现了您拥有的类,并使用了您提供的示例xml。
我创建了一个主要功能来测试
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);
}
输出
GO Sales (nalysis)
在调试模式下检查对象p显示它具有两个具有两个和三个元素的集合。 你的代码对我来说很好。