Java – XML element with attribute and content using JAXB

javajaxbxml

How can I generate the following XML using JAXB?

<sport type="" gender="">
    sport description
</sport>

Best Answer

Annotate type and gender properties with @XmlAttribute and the description property with @XmlValue:

package org.example.sport;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class Sport {

    @XmlAttribute
    protected String type;

    @XmlAttribute
    protected String gender;

    @XmlValue;
    protected String description;

}

For More Information