smat88dd
smat88dd

Reputation: 2386

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;

Upvotes: 2

Views: 1306

Answers (1)

ollo
ollo

Reputation: 25370

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.

Upvotes: 1

Related Questions