How to Declare XML Attributes
With the annotation @XmlAttribute you can declare that a bean's property or field is represented by an attribute instead of by an element. Example (with properties):
public class Movie {
private String title;
private int releaseYear;
@XmlAttribute
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@XmlAttribute
public int getReleaseYear() {
return releaseYear;
}
public void setReleaseYear(int releaseYear) {
this.releaseYear = releaseYear;
}
}
Resulting XML Schema fragment:
<xs:complexType name="movie">
<xs:sequence/>
<xs:attribute name="releaseYear" type="xs:int" use="required"/>
<xs:attribute name="title" type="xs:string"/>
</xs:complexType>
Example XML fragment:
<ns2:movieLibrary xmlns:ns2="http://example.jarfiller.com">
<collection title="Casablanca" releaseYear="1942" />
<collection title="Dr Zhivago" releaseYear="1965" />
<collection title="Out of Africa" releaseYear="1985" />
</ns2:movieLibrary>

