简体   繁体   English

ElementList属性(Java简单框架)

[英]ElementList Attributes (Java Simple Framework)

I would like to add an attribute (with the @Path annotation) to an ElementList element ... but it doesnt seem to be doable? 我想将一个属性(带有@Path注释)添加到ElementList元素...但是它似乎不可行?

I would like this: 我要这样:

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

But can achieve only this: 但是只能做到这一点:

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

The following code gives me an "org.simpleframework.xml.core.ElementException: Element 'section' is also a path name in class qti.QuestionList": 以下代码为我提供了一个“ org.simpleframework.xml.core.ElementException:元素'section'也是类qti.QuestionList中的路径名”:

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

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

Without knowing your full code it's hard to write an exact one, but here's an example: 在不知道完整代码的情况下,很难编写准确的代码,但这是一个示例:

Question class: Question类别:

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

    // ...
}

Section class: Section类:

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

    // ...
}

Test: 测试:

Section sec = ...

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

Result: 结果:

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

If you have an assessment -element around you can map it through it's own class. 如果您有一个assessment周围的元素,您可以通过它自己的类映射它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM