XML element with attribute and content using JAXB

James picture James · Apr 1, 2011 · Viewed 131.5k times · Source

How can I generate the following XML using JAXB?

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

Answer

bdoughan picture bdoughan · Apr 1, 2011

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