Mapping Classes
In case you don't already have a schema, you need to ask yourself whether you want to write the XML Schema first and generate classes for it using xjc, or whether you want to write the Java classes with JAXB annotations, and optionally generate the XML Schema later using schemagen.
- If you just want to serialize some data in XML, and don't really care too much what the XML looks like, write the definitions in Java. It's usually more convenient and the resulting Java classes are simpler and easier to use. This is called bottom-up design.
- If you care about the XML Schema (e.g. you want to publish it), write the Schema first and use xjc. This makes it a lot easier to have a good XML schema. This is called top-down design
If you have a schema, you can use xjc on the command line to get the classes that you need. xjc is included with both JDK6 and the JAXB reference implementation. Usage:
> xjc -d target-directory -p your.package.name path-to-schema
package com.jarfiller.example;
import java.util.*;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class MovieLibrary { // root element
private List<Movie> collection;
public MovieLibrary() { // no-arg constructor required (more)
}
public MovieLibrary(List<Movie> collection) { // convenience constructor
this.collection = collection;
}
public void setCollection(List<Movie> collection) {
this.collection = collection;
}
public List<Movie> getCollection() {
return collection;
}
}
public class Movie { // normal element - annotations are optional
private String title;
private int releaseYear;
public Movie() { // no-arg constructor required (more)
}
public Movie(String title, int releaseYear) {
this.title = title;
this.releaseYear = releaseYear;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getReleaseYear() {
return releaseYear;
}
public void setReleaseYear(int releaseYear) {
this.releaseYear = releaseYear;
}
}
Shorter alternative: just use public fields. The following classes would create exactly the same XML as the property-based bean above:
@XmlRootElement
public class MovieLibrary {
public List<Movie> collection;
}
public class Movie {
public String title;
public int releaseYear;
}
This is the XML Schema that the schemagen tool will create, given the MovieLibrary class:
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="movieLibrary">
<xs:sequence>
<xs:element name="collection" type="movie" nillable="true"
minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="movie">
<xs:sequence>
<xs:element name="title" type="xs:string" minOccurs="0"/>
<xs:element name="releaseYear" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Be careful with schemagen-generated schemas though: as in this example, they do not declare a top-level element. You may need to add it yourself:
<xs:element name="movieLibrary" type="movieLibrary"/>

