background image

Running the XmlRootElement Example

<< Running the XmlAttribute Field Example | Running the XmlSchemaType Class Example >>
<< Running the XmlAttribute Field Example | Running the XmlSchemaType Class Example >>

Running the XmlRootElement Example

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 different. 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.
Building and Running the XmlRootElement Example Using NetBeans
IDE
Follow these instructions to build and run the XmlRootElement 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-xmlRootElement folder.
4. Select the Open as Main Project check box.
5. Click Open Project Folder.
6. Right-click the j2s-xmlRootElement project and select Run Project.
Building and Running the XmlRootElement Example Using Ant
To compile and run the XmlRootElement example using Ant, in a terminal window, go to the
tut-install/javaeetutorial5/examples/jaxb/j2s-xmlRootElement/ directory and type the
following:
ant runapp
XmlSchemaType Class Example
The 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 data types.
Java-to-Schema Examples
Chapter 17 · Binding between XML Schema and Java Classes
545