background image

XmlAccessorOrder Example

<< Create Marshal Example | Class Content Order >>
<< Create Marshal Example | Class Content Order >>

XmlAccessorOrder Example

ant runapp
XmlAccessorOrder Example
The j2s-xmlAccessorOrder example shows how to use the @XmlAccessorOrder and
@XmlType.propOrder
annotations to dictate the order in which XML content is
marshalled/unmarshalled by a Java type.
With Java-to-schema mapping, a JavaBean's properties and fields are mapped to an XML
Schema type. The class elements are mapped to either an XML Schema complex type or an
XML Schema simple type. The default element order for a generated schema type is currently
unspecified because Java reflection does not impose a return order. The lack of reliable element
ordering negatively impacts application portability. You can use two annotations,
@XmlAccessorOrder
and @XmlType.propOrder, to define schema element ordering for
applications that need to be portable across JAXB Providers.
Using the @XmlAccessorOrder Annotation to Define Schema Element
Ordering
The @XmlAccessorOrder annotation imposes one of two element ordering algorithms,
AccessorOrder.UNDEFINED
or AccessorOrder.ALPHABETICAL. AccessorOrder.UNDEFINED is
the default setting. The order is dependent on the system's reflection implementation.
AccessorOrder.ALPHABETICAL
orders the elements in lexicographic order as determined by
java.lang.String.CompareTo(String anotherString)
.
You can define the @XmlAccessorOrder annotation for annotation type ElementType.PACKAGE
on a class object. When the @XmlAccessorOrder annotation is defined on a package, the scope
of the formatting rule is active for every class in the package. When defined on a class, the rule is
active on the contents of that class.
There can be multiple @XmlAccessorOrder annotations within a package. The order of
precedence is the innermost (class) annotation takes precedence over the outer annotation. For
example, if @XmlAccessorOrder(AccessorOrder.ALPHABETICAL) is defined on a package and
@XmlAccessorOrder(AccessorOrder.UNDEFINED)
is defined on a class in that package, the
contents of the generated schema type for the class would be in an unspecified order and the
contents of the generated schema type for every other class in the package would be alphabetical
order.
Using the @XmlType Annotation to Define Schema Element Ordering
The @XmlType annotation can be defined for a class. The annotation element propOrder() in
the @XmlType annotation allows you to specify the content order in the generated schema type.
When you use the @XmlType.propOrder annotation on a class to specify content order, all
public properties and public fields in the class must be specified in the parameter list. Any
public property or field that you want to keep out of the parameter list must be annotated with
@XmlAttribute
or @XmlTransient annotation.
Java-to-Schema Examples
The Java EE 5 Tutorial · September 2007
538