background image

Running the XmlSchemaType Class Example

<< Running the XmlRootElement Example | Running the XmlType Example >>
<< Running the XmlRootElement Example | Running the XmlType Example >>

Running the XmlSchemaType Class Example

When the @XmlSchemaType annotation is defined at the package level, the identification
requires both the XML built-in type name and the corresponding Java type class. An
@XmlSchemaType
definition on a field or property takes precedence over a package definition.
The XmlSchemaType Class example shows how to use the @XmlSchemaType annotation at the
package level, on a field, and on a property. File TrackingOrder has two fields, orderDate and
deliveryDate
, which are defined to be of type XMLGregorianCalendar. The generated schema
will define these elements to be of XML built-in type gMonthDay. This relationship was defined
on the package in the file package-info.java. Field shipDate in file TrackingOrder is also
defined to be of type XMLGregorianCalendar, but the @XmlSchemaType annotation statements
override the package definition and specify the field to be of type date. Property method
getTrackingDuration
defines the schema element to be defined as primitive type duration
and not Java type String.
Building and Running the XmlSchemaType Class Example Using
NetBeans IDE
Follow these instructions to build and run the XmlSchemaType Class example on your
Application Server instance using the NetBeans IDE.
1. In NetBeans IDE, select File
Open Project.
2. In the Open Project dialog, navigate to tut-install/javaeetutorial5/examples/jaxb/.
3. Select the j2s-xmlSchemaType-class folder.
4. Select the Open as Main Project check box.
5. Click Open Project Folder.
6. Right-click the j2s-xmlSchemaType-class project and select Run Project.
Building and Running the XmlSchemaType Class Example Using Ant
To compile and run the XmlSchemaType Class example using Ant, in a terminal window, go to
the tut-install/javaeetutorial5/examples/jaxb/j2s-xmlSchemaType-class/ directory and
type the following:
ant runapp
XmlType Example
The XmlType example demonstrates the use of the @XmlType annotation. The @XmlType
annotation maps a class or an enum type to a XML Schema type.
A class must have either a public zero-argument constructor or a static zero-argument factory
method in order to be mapped by this annotation. One of these methods is used during
unmarshalling to create an instance of the class. The factory method may reside within in a
factory class or the existing class.
Java-to-Schema Examples
The Java EE 5 Tutorial · September 2007
546