Форматирование XML списка объектов с использованием jaxb - PullRequest
1 голос
/ 29 июля 2011

Я новичок в Java и jaxb, поэтому я уверен, что проблема здесь очевидна.

Я сортирую объект, который выглядит следующим образом:

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;
    }
}

Мне нужен XML, который выглядит следующим образом:

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

Но я получаю XML, который выглядит так:

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

Все выглядело хорошо в случае с одним получателем, поэтому (я думаю) именно поэтому он был реализован таким образом. Любая помощь? Некоторое время я настраивал привязки, но ничего не работает, как я догадываюсь.

1 Ответ

4 голосов
/ 29 июля 2011

Это то, что вам нужно:

public class Annotation {

    private List<Recipient> recipients;

    @XmlElementWrapper(name="recipients")
    @XmlElement(name="recipient")
    public List<Recipient> getRecipients() {
        return recipients;
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...