Wizard API updated!
Tim Boudreau has released a new version of the Swing Wizard library (version 0.997) that fixes the WizardException bug reported in JavaWorld's recent Open Source Java Project profile. The article's examples have been reworked to test out the new, improved WizardException. Thanks, Tim, for this helpful fix!
Open Source Java Projects: The Wizard API

Newsletter sign-up

Sign up for our technology specific newsletters.

Enterprise Java
View all newsletters

Email Address:

Java-XML mapping made easy with JAXB 2.0

The new Java Architecture for XML Binding makes it easy to incorporate XML data and processing into Java applications

The Java Architecture for XML Binding provides a powerful and practical way of working with XML content from within Java applications. The newly released JAXB 2.0 offers many new features, including full support of all XML Schema features, significantly fewer generated classes, generated classes that are easier to manipulate, and a more flexible validation mechanism.

To understand how to process XML documents in Java with JAXB 2.0, we need to look at the two main JAXB components:

  • The binding compiler, which binds a given XML schema to a set of generated Java classes
  • The binding runtime framework, which provides unmarshalling, marshalling, and validation functionalities

The JAXB binding compiler (or xbj) lets you generate Java classes from a given XML schema. The JAXB binding compiler transforms an XML schema into a collection of Java classes that match the structure described in the XML schema. These classes are annotated with special JAXB annotations, which provide the runtime framework with the mappings it needs to process the corresponding XML documents.

The binding runtime framework provides an efficient and easy-to-use mechanism for unmarshalling (or reading) and marshalling (or writing) XML documents. It lets you transform an XML document into a hierarchy of Java objects (unmarshalling) or, inversely, transform a Java object hierarchy into XML format (marshalling). The term marshalling traditionally refers to disposing troops in some suitable manner. In networking, it refers to placing data items into a buffer before sending them over a communication channel.

Combined, these two components produce a technology that lets Java developers easily manipulate XML data in the form of Java objects, without having to know the nitty-gritty details of the Simple API for XML Processing (SAX) or the Document Object Model (DOM), or even the subtleties of XML Schema.

JAXB prerequisites

To get started with JAXB 2.0 you need:

  • Java Platform, Standard Edition 5: JAXB 2.0 relies heavily on features of Java SE 5, such as annotations and generics
  • An implementation of JAXB 2.0

This article was written using the GlassFish JAXB reference implementation release candidate.

Generate Java classes using the JAXB compiler

The JAXB compiler binds an XML schema to a set of Java classes. An XML schema is an XML document that describes, very precisely, the elements and attributes authorized in a certain type of XML document. In this example, we use a training course booking system that can accept orders in XML format. A typical order looks like this:

   <?xml version="1.0" encoding="UTF-8"?>
    <booking xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <company name="ACME Consulting">
            <address>10 Coyote Avenue,  Arizona, USA</address>
            <contact name="Duke" email="duke@acme.com" telephone="1234567890"/>
        </company>
        <student firstName="Jane" surname="Dow"/>
        <student firstName="John" surname="Doe"/>
    </booking>  


The corresponding XML schema describes how the training course is booked, and contains details of the booked course, the enrolled students, the company making the booking, and so forth. An XML schema description is extremely rigorous and can include details such as the number of elements allowed in a list of objects (cardinality), optional and mandatory attributes, and more. The schema for the training course bookings (called course-booking.xsd) is shown here:

1 | 2 | 3 | 4 |  Next >

Discuss

Start a new discussion or jump into one of the threads below:

Subject Replies Last post
. Marshal to file
By Nandan
0 04/25/07 04:48 AM
by Nandan
. XMLBeans
By JSS
2 10/29/06 08:30 AM
by Anonymous
. Java-XML mapping made easy
By JavaWorldAdministrator
0 10/05/06 08:31 AM
by JavaWorld


Resources