Skip to main content

Show me the code: A taste of JAXB

Posted by satyak on March 20, 2004 at 1:29 PM PST

The following sample code is extracted from "Architecture for XML Binding (JAXB) by Ed Ort and Bhakti Mehta, March 2003" to show you a taste of how jaxb coding look like with some of my own comments pepperred in.

Generating classes

xjc.sh -p test.jaxb books.xsd -d work

From books.xsd generate required interfaces and implementations in a package called "test.jaxb" and place it in a directory called "work".

Convert XML to Java (Unmarshall)

import javax.xml.bind.*;

//Get the context to work on
JAXBContext jc = JAXBContext.newInstance("test.jaxb");

Unmarshaller unmarshaller = jc.createUnmarshaller();

//The following is a generated test.jaxb.Collection
Collection collection= (Collection)
    unmarshaller.unmarshal(new File( "books.xml"));

//Using the object
CollectionType.BooksType booksType = collection.getBooks();
List bookList = booksType.getBook();

//Write the object back to xml
Marshaller marshaller = jc.createMarshaller();
marshaller.marshall(collection,
   new FileOutputStream("jaxbOutput.xml"));

As a JAXB client there are mainly three objects. Context, marshaller, and unmarshaller. The process is quite starightforward and easy. Nevertheless obtaining the JAXBContext is quite fishy. Through out the J2EE API getting provider classes for a given API is quite inconsistent. In this case you have to pass the package names to the static function of "newInstance" to locate the provider for the "JAXBContext". Passing package names for locating implementation objects seem odd. I have read the explanations in the java api for it. I am not convinced if this is the best way to obtain an implementation for a given interface.

Convert Java to XML (Marshall)

ObjectFactory objFactory = new ObjectFactory();

//Create object shells
Collection collection=
   (Collection) objFactory.createCollection();

Collection.BooksType booksType = objFactory.
   createCollectionTypeBooksType();

BookType book = objFactory.createBookType(); 

//set data items
book.setItemId("307");
book.setName("JAXB today and beyond");

//Validate collection (optional. If needed)
Validator validator = jaxbContext.createValidator();
validator.validate(collection));

//get a marshaller
Marshaller marshaller = jaxbContext.createMarshaller();

//specify output behavior
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
   new Boolean(true));

//write out xml
marshaller.marshal(collection,
   new FileOutputStream("jaxbOutput.xml"));

The interesting point to make by looking at this code is the nature in which objects are instantiated. This is done through an intermediate object factory and not by newing them. One of the reasons for this is that the implementations are not available to the developer and could be provider dependent.

References


1. Java architecture for XML binding by Ed Ort and Bhakti Mehta, March 2003

Provides an executive summary, architecture, and some sample code for JAXB


2. State of JAXB by Satya Komatineni

This is a brief write up on on the overal state of Jaxb that I have done recently. This would be useful for Architects, Managers and decission makers.


3. XML Knoweldge Folder by Satya Komatineni

A series of links, sample code, and articles on XML related technologies. This content is dynamic and may change as I update this folder as I find new things and organize existing items.

Related Topics >>