How to annotate a list of string lists using JAXB?

Say I have the following:

@XmlRootElement(name = "foo1")
public class Foo1
{
    @XmlElementWrapper( name="answerList" )
    @XmlElement( name="answer" )
    private List<String> answerList;
}

If the instance of Foo1 is ordered, it will look like this:

<foo1>
    <answerList>
        <answer>myAnswer1</answer>
        <answer>myAnswer2</answer>
    </answerList>
</foo1>

Now, if I have the following property:

private List<List<String>> answerListsList;

How can I annotate the property above, so I will have the following XML (of course, without creating a new class to hold a list of strings)?

<foo1>
    <answerLists>
        <answerList>
            <answer>row1 myAnswer1</answer>
            <answer>row1 myAnswer2</answer>
        </answerList>
        <answerList>
            <answer>row2 myAnswerA</answer>
            <answer>row2 myAnswerB</answer>
        </answerList>
    <anserLists>
</foo1>

EDIT:

The reason that you do not want to create a new class is because I try to avoid creating too many classes. Creating a new class for each list of string lists that you may have that use different element names is not a good design in my opinion.

, . , XML:

<answerLists>
    <answerList>
        <answer>row1 myAnswer1</answer>
        <answer>row1 myAnswer2</answer>
    </answerList>
    <answerList>
        <answer>row2 myAnswerA</answer>
        <answer>row2 myAnswerB</answer>
    </answerList>
<anserLists>

<myLists>
    <myList>
        <item>row1 d1</item>
        <item>row1 d2</item>
    </myList>
    <myList>
        <item>row2 dA</item>
        <item>row2 dB</item>
    </myList>
</myLists>
+5
1

XML, , !

:

@XmlRootElement( name="foo1" )
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo1<T>
{
    @XmlElementWrapper( name="answerList" )
    @XmlElement( name="answer" )
    List<T> answerList = new ArrayList<T>();
}

- :

Foo1<Foo1<String>> ff = new Foo1<Foo1<String>>();  

XML , , XML, , !

+2

All Articles