Macejkou
Macejkou

Reputation: 686

JAXB Annotations - Marshall List<String[]>

I have this simple object:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class SimilarityInfoResult {

    private String name;

    private List<String[]> parameters;

    public SimilarityInfoResult() {
    }

    public SimilarityInfoResult(String name, List<String[]> parameters) {
        this.name = name;
        this.parameters = parameters;
    }

...

}

It is mapped as this:

   <similarityInfoResult>
        <name>SubstructureSimilarity</name>
        <parameters>
            <item>treshold</item>
            <item>Double</item>
        </parameters>
        <parameters>
            <item>numberOfResults</item>
            <item>Integer</item>
        </parameters>
    </similarityInfoResult>

Desired output:

<similarityInfoResult>
    <name>SubstructureSimilarity</name>
    <parameters>
        <parameter>
            <name>treshold</name>
            <type>Double</type>
        </parameter>
        <parameter>
            <name>results</name>
            <type>Integer</type>
        </parameter>
    </parameters>
</similarityInfoResult>

How should I do this using annotations? Is it possible? Maybe I will have to make special parameter object and List<Parameter>? Thank you

Upvotes: 4

Views: 1881

Answers (1)

BobTheBuilder
BobTheBuilder

Reputation: 19284

You need to add Parameter class to hold name and type. And change List<String[]> to List<Parameter>.

This way you can more easily control the XML parsing of the parameter object.

And use:

@XmlElementWrapper(name="parameters")
@XmlElement(name="parameter")
private List<Parameter> parameters;

and:

public class Parameter{

    private String name;
    private String type;
...

}

Upvotes: 5

Related Questions