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 :

Runtime Libraries

First, you'll need to download the Java Web Services Developer Pack 2.0, which includes an implementation of JAXB 2.0. Install JWSDP 2.0 in the C:\Sun\jwsdp-2.0, the default installation directory. JAXB 2.0 gets installed in the C:\Sun\jwsdp-2.0\jaxb directory. Add the C:\Sun\jwsdp-2.0\jaxb\bin directory to the PATH variable. Because JAXB 2.0 uses the JDK5.0 feature parameterized types, you'll need to also install JDK 5.0. Add the environment variables JAVA_HOME and JAXB_HOME. JAVA_HOME specifies the directory in which J2SE 5.0 is installed. JAXB_HOME specifies the directory in which JAXB 2.0 is installed. Table 1 lists the JAR files required for JAXB 2.0.

JAR File

Description

C:/Sun/jwsdp-2.0/jaxb/lib/jaxb-api.jar

JAXB 2.0 API classes

C:/Sun/jwsdp-2.0/jaxb/lib/jaxb-impl.jar

JAXB 2.0 implementation classes

C:/Sun/jwsdp-2.0/jaxb/lib/jaxb-xjc.jar

JAXB 2.0 Compiler classes

C:/Sun/jwsdp-2.0/jwsdp-shared/lib/activation.jar

javax.activation package classes.

C:/Sun/jwsdp-2.0/sjsxp/lib/jsr173_api.jar

StAX API classes

C:/Sun/jwsdp-2.0/sjsxp/lib/sjsxp.jar

StAX classes


Table 1. JAXB 2.0 JAR Files

In comparison, the JAXB 1.0 libraries are listed below:


<JWSDP1.6>/jaxb/lib/jaxb-api.jar 
<JWSDP1.6>/jaxb/lib/jaxb-impl.jar
<JWSDP1.6>/jaxb/lib/jaxb-libs.jar 
<JWSDP1.6>/jaxb/lib/jaxb-xjc.jar 
<JWSDP1.6>/jwsdp-shared/lib/namespace.jar 
<JWSDP1.6>/jwsdp-shared/lib/jax-qname.jar
<JWSDP1.6>/jwsdp-shared/lib/relaxngDatatype.jar

Compiling an XML Schema

Listing 1 shows an example XML schema that is compiled using the JAXB 2.0 schema binding compiler .xjc.

The schema binding compiler is used to generate Java classes from an XML schema. Run the schema binding compiler with the following command.


>xjc catalog.xsd
This generates Java classes in the generated package:

parsing a schema...
compiling a schema...
generated\ArticleType.java
generated\CatalogType.java
generated\JournalType.java
generated\ObjectFactory.java
As you can see, fewer Java classes are generated. JAXB 2.0 generates a Java value class that corresponds to each top-level complexType. For example, for catalogType, 2.0 generates the Java value class CatalogType.java. CatalogType.java consists of getter and setter methods for each of the elements and attributes in the complexType. As the catalog element may have more than one journal element, the return type of getJournal() mehod is java.util.List<JournalType>.

Similarly, 2.0 generates the Java value class ArticleType.java, which corresponds to complexType articleType. It also generates JournalType.java, which further corresponds to complexType journalType. Also generated is the ObjectFactory.java class, consisting of create methods which create Java value class objects for ArticleType.java, CatalogType.java, and JournalType.java. The factory class also contains a create method corresponding to each of the top-level elements.

In contrast, JAXB 1.0 generates an interface and an implementation class for each complexType and top-level element. When the XML schema compiled in Listing 1 is compiled by JAXB 1.0, it generates all the classes shown in Listing 2—significantly more than what is generated by JAXB 2.0.

Support for All XML Schema Constructs

JAXB 1.0 does not support key XML Schema components like any, anyAttribute, key, keyref, and unique. It also does not support attributes like complexType.abstract, element.abstract, element.substitutionGroup, xsi:type, complexType.block, complexType.final, element.block, element.final, schema.blockDefault, and schema.finalDefault. In version 2.0, support has been added for all of these schema constructs.

To see how it works, first define an XML document consisting of the xs:any and xs:anyAttribute schema constructs:


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <xsd:element name="catalog" type="catalogType"/>

 <xsd:complexType name="catalogType">
  <xsd:sequence>
   <xsd:any/>
  </xsd:sequence>
  <xsd:attribute name="journal" type="xsd:string"/>
  <xsd:attribute name="publisher" type="xsd:string"/>
  <xsd:anyAttribute/>
 </xsd:complexType>

</xsd:schema>
Next, compile the XML document using schema compiler xjc:

>xjc catalog.xsd
The output from the schema compilation is shown below:

parsing a schema...
compiling a schema...
generated\CatalogType.java
generated\ObjectFactory.java
In the schema-derived class CatalogType.java, the xs:any element is mapped with the @XmlAnyElement annotation and the xs:anyAttribute is mapped with the @XmlAnyAttribute annotation. JAXB 2.0 provides support for all the XML schema constructs in the schema, allowing you to a wider range of schemas and documents.

Home / Articles / Improved XML Binding with JAXB 2.0 / 1 / 2 / Next Page

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.

 Avaya DevConnect Center
 Service Component Architecture/Service Data Objects Solution Center
 Intel Go Parallel Portal
 Internet.com eBook Library
 IBM Software Construction Toolbox
 Microsoft RIA Development Center
 Destination .NET
XML error: not well-formed (invalid token) at line 53
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%.

Latest Linux Hits Networking Flaws
Metasploit 3.2 Offers More 'Evil Deeds'
'Thank You Apple. Seriously.'
The Buzz: BlackBerry App Store Seen Next
Is .NET on Linux Finally Ready?
Red Hat Takes on HPC Market, Microsoft
Python's New Release Bridges the Gap
No Flash Seen on iPhone Horizon
Apple Yields to Complaints Over iPhone NDA
Microsoft Shows Some Ankle With Visual Studio

Use Explicit Conversion Functions to Avert Reckless Implicit Conversions
Polyglot Programming: Building Solutions by Composing Languages
Automated testing for .NET by Ben Hall
"Supply Chain" SOA with SKOS
Service Component Architecture in Real Life
C++Ox: The Dawning of a New Standard
Getting Started with Virtualization
Master Complex Builds with MSBuild
eCryptfs: Single-File Encryption in Linux
CCXML in Action: A CCXML Auto Attendant

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

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Tripwire Whitepaper: Seven Practical Steps to Mitigate Virtualization Security Risks
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES