Madi Sagimbekov
Madi Sagimbekov

Reputation: 319

JAXB XmlElement without entity class

I want to create xml with this kind of structure using JAXB marshaller.

<A> 
  <Bs> 
    <B> </B> 
    <B> </B> 
    <B> </B> 
  <Bs>
</A>

I have to entity classes A and B.

@XmlRootElement
public class A {
    private List<B> b;

    public List<B> getB() {
        return b;
    }

    @XmlElement(name="Bs")
    public void setB(List<B> b) {
        this.b = b;
    }
}

public class B {} 

but when I initialize class A and marshal, I get

<A>
    <Bs> </Bs> 
    <Bs> </Bs> 
    <Bs> </Bs> 
</A>

How to get desired xml structure (see first xml in this page)?

Upvotes: 1

Views: 529

Answers (1)

Grzegorz Grzybek
Grzegorz Grzybek

Reputation: 6237

Use javax.xml.bind.annotation.XmlElementWrapper annotation:

@XmlElementWrapper(name = "Bs")
@XmlElement(name="B")
public void setB(List<B> b) {
    this.b = b;
}

Upvotes: 2

Related Questions