prev table of contents next

6 JAXB Annotations

6.1 How a Schema Mapping Is Implemented

The Java code generated by the JAXB schema compiler contains annotations providing metadata on packages, classes, fields and methods. Together, this metadata is intended to reflect the information contained in an XML schema, of which only a very small part can be expressed by the actual Java code.

Annotations can be easily retrieved from their target construct with methods contained in classes such as java.lang.Class or java.lang.reflect.Field. Each annotation type has its own set of attributes, which are accessed in the usual way. Given some class, an annotation of type XmlType can be retrieved with

Class clazz = ...;
XmlType typeAnn = clazz.getAnnotation( XmlType.class );
If the result of the annotation getter is not null, annotation element values may be obtained by calling methods on the returned XmlType object. To retrieve the name of the corresponding XML Schema type you would write
String schemaName = typeAnn.name();

Classes that can be used for marshalling and unmarshalling XML need not be generated by the JAXB schema compiler. It is equally possible to write these classes by hand, adding the JAXB annotations. We'll discuss some basic annotations in the next section.


prev table of contents next