ElementList属性(Java简单框架)

时间:2014-06-16 16:18:09

标签: java xml simple-framework

我想将一个属性(带有@Path注释)添加到ElementList元素......但它似乎不可行吗?

我想这样:

<section title="traaa">
    <item title="a" />
    <item title="b" />
</section>

但只能实现这一点:

<section>
    <item title="a" />
    <item title="b" />
</section>

以下代码为我提供了一个&#34; org.simpleframework.xml.core.ElementException:Element&#39;部分&#39;也是类qti.QuestionList&#34;中的路径名:

@Attribute(name="title")
@Path("assessment/section")
public String title1;

@ElementList(name="section")
@Path("assessment")
public ArrayList<Question> qsts;

1 个答案:

答案 0 :(得分:1)

在不知道完整代码的情况下,编写一个完整的代码很难,但这里有一个例子:

Question类:

@Root(name = "item")
public class Question
{
    @Attribute(name = "title")
    private String title;

    // ...
}

Section类:

@Root(name = "section")
public class Section
{
    @Attribute(name = "title")
    private String title;
    @ElementList(name = "items", inline = true)
    private List<Question> qsts;

    // ...
}

测试:

Section sec = ...

Serializer ser = new Persister();
ser.write(sec, System.out);

结果:

<section title="traaa">
   <item title="a"/>
   <item title="b"/>
</section>

如果你有一个assessment元素,你可以将它映射到它自己的类。