10.12.2012 Views

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

The Java EE 5 Tutorial (PDF) - Oracle Software Downloads

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Java</strong>-to-Schema Examples<br />

544<br />

When the @XmlSchemaType annotation is defined at the package level, the identification<br />

requires both the XML built-in type name and the corresponding <strong>Java</strong> type class. An<br />

@XmlSchemaType definition on a field or property takes precedence over a package definition.<br />

<strong>The</strong> XmlSchemaType Class example shows how to use the @XmlSchemaType annotation at the<br />

package level, on a field, and on a property. File TrackingOrder has two fields, orderDate and<br />

deliveryDate, which are defined to be of type XMLGregorianCalendar. <strong>The</strong> generated schema<br />

will define these elements to be of XML built-in type gMonthDay. This relationship was defined<br />

on the package in the file package-info.java. Field shipDate in file TrackingOrder is also<br />

defined to be of type XMLGregorianCalendar, but the @XmlSchemaType annotation statements<br />

override the package definition and specify the field to be of type date. Property method<br />

getTrackingDuration defines the schema element to be defined as primitive type duration<br />

and not <strong>Java</strong> type String.<br />

Building and Running the XmlSchemaType Class Example Using<br />

NetBeans IDE<br />

Follow these instructions to build and run the XmlSchemaType Class example on your<br />

Application Server instance using NetBeans IDE.<br />

1. In NetBeans IDE, select File→Open Project.<br />

2. In the Open Project dialog, navigate to tut-install/javaeetutorial5/examples/jaxb/.<br />

3. Select the j2s-xmlSchemaType-class folder.<br />

4. Select the Open as Main Project check box.<br />

5. Click Open Project.<br />

6. Right-click the j2s-xmlSchemaType-class project and select Run.<br />

Building and Running the XmlSchemaType Class Example Using Ant<br />

To compile and run the XmlSchemaType Class example using Ant, in a terminal window, go to<br />

the tut-install/javaeetutorial5/examples/jaxb/j2s-xmlSchemaType-class/ directory and<br />

type the following:<br />

ant runapp<br />

XmlType Example<br />

<strong>The</strong> XmlType example demonstrates the use of the @XmlType annotation. <strong>The</strong> @XmlType<br />

annotation maps a class or an enum type to a XML Schema type.<br />

A class must have either a public zero-argument constructor or a static zero-argument factory<br />

method in order to be mapped by this annotation. One of these methods is used during<br />

unmarshalling to create an instance of the class. <strong>The</strong> factory method may reside within in a<br />

factory class or the existing class.<br />

<strong>The</strong> <strong>Java</strong> <strong>EE</strong> 5<strong>Tutorial</strong> • June 2010

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!