Java Architecture for XML Binding (JAXB) 1.0 made it easier for developers to manipulate XML content from Java applications. This is achieved by specifying a binding of a XML document to JavaBeans objects based on the XML document's XSD schema. The binding is easy to use and natural to a Java programmer. These APIs have been used to successfully process some large, well-known schemas.
JAXB 2.0 offers new functionality and enables the development and deployment of JAXB applications in an even wider range of environments. JAXB 2.0 is backward compatible with JAXB 1.0, which is important for legacy applications.
How to set up JAXB 2.0
You will need to download the Java Web Services Developer Pack (Java WSDP) 2.0, which includes an implementation of JAXB 2.0. JAXB 2.0 uses parameterized types (which are a JDK 5.0 feature), so you will also need to install JDK 5.0 if you have not already.
JAXB 2.0's features
The new features in the latest version of JAXB facilitate the marshalling and unmarshalling of an XML document. Marshalling an XML document is when you create an XML document using the Java classes generated by compiling an XML schema. Unmarshalling an XML document is when you create a Java representation of an XML document and, subsequently, retrieve element and attribute values in the XML document.
With JAXB 2.0, you can map a Java object to an XML document or an XML schema. Some of the other new features and benefits in JAXB 2.0 include the following:
- Requires smaller runtime libraries, which require less runtime memory.
- Generates significantly fewer Java classes from a schema compared to JAXB 1.0. For each top-level complexType, JAXB 2.0 generates a value class instead of an interface and an implementation class. For each top-level element, JAXB 2.0 generates a Factory class method instead of an interface and an implementation class.
- Includes support for all of XML schema constructs.
- Includes added parameterized types.
- Deprecates the UnMarshaller interface method setValidating(), which has been replaced with JAXP 1.3 validation.
- Includes support for binding Java-to-XML with the javax.xml.bind.annotation package.
How JAXB 2.0 works
To understand how to process XML documents in Java with JAXB 2.0, here's a closer look at the two main JAXB components: the binding compiler (which binds a given XML schema to a set of generated Java classes) and the binding runtime framework (which provides unmarshalling, marshalling, and validation functionalities).Binding compiler
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.
Marshaller and unmarshaller combined produce a technology that lets Java developers easily manipulate XML data in the form of Java objects — without having to know all of the details of the Simple API for XML Processing (SAX) or the Document Object Model (DOM) or even the subtleties of XML schema.
This is a sample XML document that I will process:
<?xml version="1.0" encoding="UTF-8"?>
<company name="ABC Bank">
<address>60 Wall Street, New York, NY</address>
<contact name="Peter Mikhalenko" email="email@example.com" telephone="1234567890"/>
<flight from="New York" to="San Francisco"/>
<flight from="Los Angeles" to="New York"/></ticket>
If, for example, the schema for this file contained a ticket with two flights called tickets.xsd, you could run the command line tool xjc to run the JAXB compiler, like this:
$xjc tickets.xsd -p fullclassname.ticket -d src/generated
This will generate a set of Java classes annotated with JAXB 2.0 annotations.Binding runtime framework
To unmarshall an XML document, you create an unmarshaller from the context. The unmarshaller can process XML data from a wide variety of data sources, including files, input streams, URLs, DOM objects, SAX parsers, and more.
For this next code sample, create a JAXBContext context object (the context object is the starting point for marshalling, unmarshalling, and validation operations). Here is a simple File object pointing to our XML document. The unmarshaller returns a typed JAXBElement from which you can obtain the unmarshalled object by using the getValue() method:
Unmarshaller unmarshaller =
= (JAXBElement<Ticket>) unmarshaller.unmarshal(
new File("src/test/resources/xml/ticket.xml"));Ticket ticket = ticketElement.getValue();
Marshalling involves transforming your Java classes into XML format. In JAXB 2.0, it's simple to create and manipulate these Java classes. You can treat XML as an ordinary class (according to the corresponding XML schema), as shown in the example below. Once your domain object is initialized, use the JAXB context to create a Marshaller object and a typed JAXBElement. Next, create a JAXBElement object that encapsulates your domain object. The typed JAXBElement corresponds to the root element complexType of your XML document. Then use the generated ObjectFactory class as follows:
....Ticket ticket = new Ticket();
ticket.setFlight("New York","San Francisco");
....ObjectFactory factory = new ObjectFactory();
Ticket booking = factory.createTicket();
....Marshaller marshaller = jaxbContext.createMarshaller();
....JAXBElement<Ticket> ticketElement = (new ObjectFactory()).createTicket(ticket);
....marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);marshaller.marshal( ticketElement, System.out );
JAXB 2.0 simplifies the use of XML content in Java applications. JAXB does this by specifying a binding between an XML document and JavaBeans technology components based on the XML document's XSD schema.
If you want to learn more about this topic, I recommend that you check out the JAXB 2.0 Reference Implementation Project.
Peter V. Mikhalenko is a Sun certified professional who works for Deutsche Bank as a business consultant.
———————————————————————————————————————————-Get Java tips in your inbox Delivered each Thursday, our free Java newsletter provides insight and hands-on tips you need to unlock the full potential of this programming language. Automatically subscribe today!