简体   繁体   中英

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?

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":

@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:

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

    // ...
}

Section class:

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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