Feeds

A practical guide to JAXB 2.0

Take a peek at its new features

  • alert
  • submit to reddit

Boost IT visibility and business value

JSR222 specifies the Java Architecture for XML Binding (JAXB) 2.0. JAXB 2.0 specification is implemented in Java Web Services Developer Pack (JWSDP) 2.0. JAXB 2.0 has some new features that facilitate the marshalling and unmarshalling of an XML document. I have used both JAXB 1.0 and JAXB 2.0 and have found that JAXB 2.0 generates less code and has some additional features.

How is JAXB 2.0 better than JAXB 1.0?

  1. JAXB 2.0 uses parameterized types, a new feature in JDK 5.0. The advantage of parameterized types is compile time type checking. JAXB 2.0 also uses annotations. Annotations provide a metadata facility with which code may be generated using annotation types and annotation declarations.
  2. JAXB 2.0 supports all of XML Schema constructs, JAXB 1.0 doesn't.
  3. With JAXB 2.0 fewer Java classes are generated from an XML Schema as compared to JAXB 1.0. For each top level complexType, a value class is generated, instead of an interface and an implementation class. For each top-level element, a factory class method is generated, instead of an interface and an implementation class.
  4. Fewer runtime libraries are required for JAXB 2.0.
  5. In JAXB 2.0, support for binding Java to XML has been added with javax.xml.bind.annotation package.
  6. JAXB 2.0 provides integration with the Java API for XML-based Web Services (JAX-WS) 2.0.

Comparison of JAXB 2.0 with DOM and SAX

Both JAXB 2.0 and DOM APIs provide random access to the XML Infoset. In a performance comparison the DOM API as implemented in Apache Xerces was found to be 20 per cent faster than the JAXB 2.0 API. The SAX API has the advantage of being a event-based streaming API and is therefore less memory intensive. But, the SAX API does not offer random access of the XML Infoset. JAXB 2.0 combines the advantages of the random access processing model and the event based streaming processing model with unmarshal() methods that unmarshal from an XMLStreamReader or XMLEventReader object.

Overview

In this article we shall discuss the new features of JAXB 2.0. We shall marshal and unmarshal an XML document with JAXB 2.0 API. Marshalling an XML document is creating an XML document using the Java classes generated by compiling an XML schema. Unmarshalling an XML document is creating a Java representation of an XML document, and subsequently retrieving element and attribute values in the XML document. An example XML schema, catalog.xsd (resources zip file), shall be compiled with JAXB 2.0 binding compiler. An example XML document, catalog.xml, shall be marshalled and unmarshalled using the Java classes generated from the XML Schema.

Preliminary Setup

Download JWSDP 2.0, which includes an implementation of JAXB 2.0. Install JWSDP 2.0 in the C:\Sun\jwsdp-2.0 directory, the default installation directory. JAXB 2.0 gets installed in the C:\Sun\jwsdp-2.0\jaxb directory. As JAXB 2.0 uses parameterized types, which is a JDK 5.0 feature, you will also need to install JDK 5.0.

Install the Eclipse IDE. Create a new project JAXB2 with File>New>Project. Next, create a Java package, com.register.jaxb with File>New>Package. In the Java package create a Java class, JAXBMarshaller.java, with File>New>Class.

Similarly, create Java classes JAXBUnMarshaller.java and JavaToXML.java. JAXBMarshaller.java is used to marshal an XML document and JAXBUnMarshaller.java is used to unmarshal an XML document. JavaToXML.java is used to map a Java class consisting of Java-to-XML binding annotations to an XML document.

Copy the code from JAXBMarshaller.java, JAXBUnMarshaller.java and JavaToXML.java (resources zip file) to the Eclipse project. Import catalog.xml to the JAXB2 project with File>Import. Create a gen_source folder in the JAXB2 project with File>New>Folder and Import catalog.xsd to the gen_source folder. Add the gen_source folder to project build path by selecting Project Properties.

In the Properties frame select the Java Build Path. Select the Source tab and add the gen_source folder with Add Folder. To compile and run JAXB 2.0 applications, we need some JAXB 2.0 JAR files in the Java Build Path. We also need to set JAXB2 project JRE to J2SE 5.0 JRE. JAXB2 project Java Build Path is as shown in Figure 1.

JAXB Project Directory Build Path

Build a business case: developing custom apps

More from The Register

next story
KDE releases ice-cream coloured Plasma 5 just in time for summer
Melty but refreshing - popular rival to Mint's Cinnamon's still a work in progress
Leaked Windows Phone 8.1 Update specs tease details of Nokia's next mobes
New screen sizes, dual SIMs, voice over LTE, and more
Mozilla keeps its Beard, hopes anti-gay marriage troubles are now over
Plenty on new CEO's todo list – starting with Firefox's slipping grasp
Apple: We'll unleash OS X Yosemite beta on the MASSES on 24 July
Starting today, regular fanbois will be guinea pigs, it tells Reg
Another day, another Firefox: Version 31 is upon us ALREADY
Web devs, Mozilla really wants you to like this one
Secure microkernel that uses maths to be 'bug free' goes open source
Hacker-repelling, drone-protecting code will soon be yours to tweak as you see fit
Cloudy CoreOS Linux distro declares itself production-ready
Lightweight, container-happy Linux gets first Stable release
prev story

Whitepapers

Implementing global e-invoicing with guaranteed legal certainty
Explaining the role local tax compliance plays in successful supply chain management and e-business and how leading global brands are addressing this.
Boost IT visibility and business value
How building a great service catalog relieves pressure points and demonstrates the value of IT service management.
Why and how to choose the right cloud vendor
The benefits of cloud-based storage in your processes. Eliminate onsite, disk-based backup and archiving in favor of cloud-based data protection.
The Essential Guide to IT Transformation
ServiceNow discusses three IT transformations that can help CIO's automate IT services to transform IT and the enterprise.
Maximize storage efficiency across the enterprise
The HP StoreOnce backup solution offers highly flexible, centrally managed, and highly efficient data protection for any enterprise.