tallseth
tallseth

Reputation: 3665

Formatting the XML of a list of objects using jaxb

I'm new to Java and jaxb, so I'm sure the problem here is obvious.

I'm marshalling an object that looks like this:

public class Annotation {

    private RecipientCollection recipients;

    @XmlElement
    public RecipientCollection getRecipients() {
        return recipients;
    }
}

@XmlRootElement( name="Recipient" )
public class RecipientCollection extends ArrayList<Recipient> {
    @XmlElement(name="Recipient")
    private RecipientCollection recipients = this;
}

@XmlElement( name="Recipient" )
public class Recipient {

    private String value;
    private String name;

    @XmlElement( name="Recipient" )
    public String getValue() {
        return value;
    }
}

I need XML that looks like this:

<Annotation>
<recipients>
  <recipient>
    <RecipientName>test</RecipientName>
    <Recipient>test</Recipient>
  </recipient>
  <recipient>
    <RecipientName>test</RecipientName>
    <Recipient>test</Recipient>
  </recipient>
</recipients>

But I get XML that looks like this:

<Annotation>
<DeletableBy>Administrator,Recipient</DeletableBy>
<recipients>
    <RecipientName>test</RecipientName>
    <Recipient>test</Recipient>
</recipients>
<recipients>
    <RecipientName>test</RecipientName>
    <Recipient>test</Recipient>
</recipients>

Everything looked fine in the single recipient case, which (I think) is why it was implemented this way in the first place. Any help? I've been tweaking the bindings around for a while, but nothing works like I'm guessing.

Upvotes: 1

Views: 129

Answers (1)

skaffman
skaffman

Reputation: 403581

This is what you need:

public class Annotation {

    private List<Recipient> recipients;

    @XmlElementWrapper(name="recipients")
    @XmlElement(name="recipient")
    public List<Recipient> getRecipients() {
        return recipients;
    }
}

Upvotes: 4

Related Questions