Java Reference
In-Depth Information
JAXB Annotations
While a complete discussion of JAXB is beyond the scope of this topic, it's worthwhile to
take a moment to examine the most important annotations that JAXB defines, all of which are
in the javax.xml.bind.annotation package.
JAXB annotations can be added to packages, classes, fields, or methods. Here we will exam-
ine the annotations that XJC might typically add to a Java object that it unmarshals from an
XML schema.
XMLRootElement
This annotation at the class level indicates that the Java type is usable as a root element in a
schema on marshaling from Java to XML. The Topic example defines only a complexType ,
and not an element, and therefore did not receive this annotation during unmarshaling.
Example 2-14 is a schema with a global element that uses XMLRootElement .
Example2-14.XML schema containing a root element
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://ns.soacookbook.com/book"
xmlns:tns="http://ns.soacookbook.com/book"
elementFormDefault="qualified">
<xsd:element name="author">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="firstName" type="xsd:string"/>
<xsd:element name="lastName" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
If you issue this command:
> xjc -d . AuthorRootType.xsd
JAXB will generate the Java type shown in Example 2-15 (with the appropriate package) in
the current directory.
Example2-15.Author type annotated with XMLRootElement
Search WWH ::




Custom Search