Mapping Java to XML Using Annotations
JAXB 2.0 also allows you to marshal a Java object to an XML document using annotations. You must be familiar with JDK 5.0 to use the annotations to generate an XML document. Table 2 shows some of the most commonly used annotations, as defined in the javax.xml.bind.annotation package.
|
Annotation
Type
|
Description
|
Annotation
Elements
|
|
XmlValue
|
Maps
a class to an XML Schema complex type with simpleContent or an XML Schema
simple type.
|
-
|
|
XmlType
|
Maps
a class to an XML Schema type, which may be a simple type or a complex type.
|
name-Name
of XML Schema type
namespace-Target
namespace of XML Schema type.
propOrder-Specifies
order of XML schema elements when a class is mapped to a complex type.
|
|
XmlSchema
|
Maps
a package name to a XML namespace.
|
attributeFormDefault-Specifies
value of attributeFormDefault attribute.
elementFormDefault-Specifies
value of attribute elementFormDefault.
namespace-XML
namespace
xmlns-Maps
namespace prefixes to namespace URIs.
|
|
XmlRootElement
|
Maps
a class to root element.
|
name-Local
name of root element.
namespace-Namespace
of root element.
|
|
XmlList
|
Maps
a property to a list simple type.
|
-
|
|
XmlEnum
|
Maps
an enum to simple type with enumeration.
|
value-Enumeration
value
|
|
XmlElement
|
Maps
a JavaBean property to an element.
|
defaultValue-Default
value of element.
name-Element
name
namespace-Target
namespace of element.
nillable-Specifies
if element is nillable. Default is false.
Type-Element
type.
|
|
XmlAttribute
|
Maps
a JavaBean property to an attribute.
|
name-Attribute
name.
namespace-Attribute
namespace.
required-Specifies
if attribute is required. Default is false.
|
Table 2. Some of the most commonly used annotations.
Creating an annotated class can be done in five steps:
- Create a simple class called
Catalog.java.
- Import the
javax.xml.bind.annotation package with its annotation types.
- Create the root element of an XML document with
@XmlRootElement annotation.
- Create a
complexType using @XmlType annotation.
- The
Root element catalog has the attributes publisher, edition, and journal. Define the attributes using @XmlAttribute annotation.
Listing 3 shows how to create an annotated class.
As you can see, the annotation element name is specified as an empty string, because the complexType is defined within an element. The element order is specified using propOrder annotation element.
The corresponding schema representation for example XML document is shown below:
<xs:element name="catalog">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
</xs:sequence>
<xs:attribute name="publisher" type="xs:string"/>
<xs:attribute name="edition" type="xs:string"/>
<xs:attribute name="journal" type="xs:string"/>
</xs:complexType>
</xs:element>
JAXB 2.0 provides bi-directional mapping with which an annotated Java class may be mapped to an XML Schema using the Schema Generator.
A Step in the Right Direction
JAXB 2.0's new features reduce the code generated from a schema with the schema binding compiler and make use of JDK 5.0 features such as annotations and parameterized types. With it's support for annotations, JAXB 2.0 provides bi-directional mapping between XML Schema and Java objects. Parameterized types provide compile-time type checking, which means that runtime exceptions do not get generated.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|