background image

j2s-xmlRootElement Example

<< j2s-xmlAttribute-field Example | j2s-xmlType Example >>
<< j2s-xmlAttribute-field Example | j2s-xmlType Example >>
J
2
S
-
XML
R
OOT
E
LEMENT
E
XAMPLE
73
non-collection type. It meets the requirment of being a simple type; it is an
enum type.
j2s-xmlRootElement Example
The j2s-xmlRootElement example demonstrates the use of the @XmlRootEle-
ment annotation to define an XML element name for the XML schema type of
the corresponding class.
The @XmlRootElement annotation maps a class or an enum type to an XML
element. At least one element definition is needed for each top-level Java type
used for unmarshalling/marshalling. If there is no element definition, there is no
starting location for XML content processing.
The @XmlRootElement annotation uses the class name as the default element
name. You can change the default name by using the annotation attribute
name
.
If you do, the specified name will then be used as the element name and the type
name. It is common schema practice for the element and type names to be differ-
ent. You can use the @XmlType annotation to set the element type name.
The namespace attribute of the @XmlRootElement annotation is used to define a
namespace for the element.
j2s-xmlSchemaType-class Example
The j2s-XmlSchemaType-class example demonstrates the use of the annotation
@XmlSchemaType to customize the mapping of a property or field to an XML
built-in type.
The @XmlSchemaType annotation can be used to map a Java type to one of the
XML built-in types. This annotation is most useful in mapping a Java type to
one of the nine date/time primitive datatypes.
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. A @XmlSchemaType definition on a field or property takes pre-
cedence over a package definition.
The j2s-XmlSchemaType-clasexample 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