advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : Improved XML Binding with JAXB 2.0 :

Mapping Java to XML Using Annotations

JAXB 2.0 also allows you to marshal a Java object to an XML document using annotations. You must be familiar with JDK 5.0 to use the annotations to generate an XML document. Table 2 shows some of the most commonly used annotations, as defined in the javax.xml.bind.annotation package.

Annotation Type

Description

Annotation Elements

XmlValue

Maps a class to an XML Schema complex type with simpleContent or an XML Schema simple type.

-

XmlType

Maps a class to an XML Schema type, which may be a simple type or a complex type.

name-Name of XML Schema type

namespace-Target namespace of XML Schema type.

propOrder-Specifies order of XML schema elements when a class is mapped to a complex type.

XmlSchema

Maps a package name to a XML namespace.

attributeFormDefault-Specifies value of attributeFormDefault attribute.

elementFormDefault-Specifies value of attribute elementFormDefault.

namespace-XML namespace

xmlns-Maps namespace prefixes to namespace URIs.

XmlRootElement

Maps a class to root element.

name-Local name of root element.

namespace-Namespace of  root element.

XmlList

Maps a property to  a list simple type.

-

XmlEnum

Maps an enum to simple type with enumeration.

value-Enumeration value

XmlElement

Maps a JavaBean property to an element.

defaultValue-Default value of element.

name-Element name

namespace-Target namespace of element.

nillable-Specifies if element is nillable. Default is false.

Type-Element type.

XmlAttribute

Maps a JavaBean property to an attribute.

name-Attribute name.

namespace-Attribute namespace.

required-Specifies if attribute is required. Default is false.


Table 2. Some of the most commonly used annotations.

Creating an annotated class can be done in five steps:

  1. Create a simple class called Catalog.java.
  2. Import the javax.xml.bind.annotation package with its annotation types.
  3. Create the root element of an XML document with @XmlRootElement annotation.
  4. Create a complexType using @XmlType annotation.
  5. The Root element catalog has the attributes publisher, edition, and journal. Define the attributes using @XmlAttribute annotation.
Listing 3 shows how to create an annotated class.

As you can see, the annotation element name is specified as an empty string, because the complexType is defined within an element. The element order is specified using propOrder annotation element.

The corresponding schema representation for example XML document is shown below:


<xs:element name="catalog">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string"/>
<xs:element name="author" type="xs:string"/>
</xs:sequence>
<xs:attribute name="publisher" type="xs:string"/>
<xs:attribute name="edition" type="xs:string"/>
<xs:attribute name="journal" type="xs:string"/>
</xs:complexType>
</xs:element>
JAXB 2.0 provides bi-directional mapping with which an annotated Java class may be mapped to an XML Schema using the Schema Generator.

A Step in the Right Direction

JAXB 2.0's new features reduce the code generated from a schema with the schema binding compiler and make use of JDK 5.0 features such as annotations and parameterized types. With it's support for annotations, JAXB 2.0 provides bi-directional mapping between XML Schema and Java objects. Parameterized types provide compile-time type checking, which means that runtime exceptions do not get generated.
Home / Articles / Improved XML Binding with JAXB 2.0 / 1 / 2 / 3 / 4 /

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

 IBM Software Construction Toolbox
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 33
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

IBM Gives a Mobile Voice to Developers
Inadequate Tools Send Software Down the Drain
USB 3.0 One Step Closer to Reality
Would-Be Linux Contributors May Get a Leg Up
SAP, Oracle Holding Out on Ubuntu?
GIPS Technology to Voice-Enable iPhone Apps
Citrix CTO Eyes the Future of Virtualization
The Pitfalls of Open Source Litigation
LiMo Open to Working With Google on Mobile
Google Gadgets Under Attack at Black Hat

The Guide to E-Mail Archiving and Management
Making XQuery Control Structures Work for You
Simplifying Composite Applications with Service Component Architecture
Overview: C++ Gets an Overhaul
Sharpening Your Axis with Visual Basic 9
Easier C++: An Introduction to Concepts
Simpler Multithreading in C++0x
Software as a Service Perceptions Survey - March 2007
Rackspace® Security
On Demand Media Technology Company Relies on The IT Hosting Leader to Provide Managed Microsoft Exchange

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About



JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers