Zakaria Bouazza
Zakaria Bouazza

Reputation: 461

Repeating tag with Jaxb

As Im beginner with xsd and jaxb, I'm quite stuck on this one, even though looked up on the net, nothing similar:

I want to facilitate in my program writing xsd using jaxb so representing an accordion with my class :

@XmlRootElement(name = "div")
@XmlType(propOrder = { "h3", "div" })
public class Accordion {

String id;

public String getId() {
    return id;
}

@XmlAttribute
public void setId(String id) {
    this.id = id;
}

String h3;

public String getH3() {
    return h3;
}

@XmlElement
public void setH3(String h3) {
    this.h3 = h3;
}

String div;

public String getDiv() {
    return div;
}

@XmlElement
public void setDiv(String div) {
    this.div = div;
}}

While marshaling an object : the result is as follows :

<div id="title 25">
    <h3>hi1</h3>
    <div>div content</div>
</div>

Now the problem is that I want h3 and div repeated inside div, something like this :

<div id="title 25">
    <h3>hi1</h3>
    <div>div content</div>
    <h3>hi2</h3>
    <div>div content 2</div>
    ...
</div>

Any idea?

Upvotes: 2

Views: 760

Answers (1)

bdoughan
bdoughan

Reputation: 149017

You can get the XML you are looking for, you just need to change your model to something like the following and leverage @XmlElementRefs/@XmlElementRef.

@XmlElementRefs({
    @XmlElementRef(name="h3", type=JAXBElement.class),
    @XmlElementRef(name="div", type=JAXBElement.class)
})
public List<JAXBElement<String>> getH3AndDiv() {
    return h3AndDiv;
}

Full Example

Upvotes: 1

Related Questions