使用jaxb格式化对象列表的XML

时间:2011-07-28 21:30:53

标签: java jaxb

我是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 个答案:

答案 0 :(得分:4)

这就是你需要的:

public class Annotation {

    private List<Recipient> recipients;

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