Thursday, May 1, 2008

JAXB

How do you access and use an XML document through the Java ? Two ways
  1. JAXP - Use Java API for XML Processing (JAXP) ( ie either SAX or DOM )
  2. JAXB - Java Architecture for XML Binding (JAXB)

we can modify the second way as two step process

  1. Bind the Schema for XML document into a set of Java classes ( and interface ) that represents the schema. All JAXB implementations provide a tool called a binding compiler to bind a schema (the way the binding compiler is invoked can be implementation-specific).
  2. Unmarshal the document into Java content objects. Unmarshalling an XML document means creating a tree of content objects that represents the content and organization of the document. The content tree is not a DOM-based tree. In fact, content trees produced through JAXB can be more efficient in terms of memory use than DOM-based trees.

The binding framework comprises three packages. The primary package, javax.xml.bind, contains classes and interfaces for performing operations such as unmarshalling, marshalling, and validation (marshalling and validation will be covered later). A second package, javax.xml.bind.util, contains a number of utility classes. The third package, javax.xml.bind.helper, is designed for JAXB implementation providers.

(1) Get Classes from XML
To unmarshal an XML document, you:

Create a JAXBContext object. This object provides the entry point to the JAXB API. When you create the object, you need to specify a context path. This is a list of one or more package names that contain interfaces generated by the binding compiler. By allowing multiple package names in the context path, JAXB allows you to unmarshal a combination of XML data elements that correspond to different schemas.

import javax.xml.bind.JAXBContext;
JAXBContext jc = JAXBContext.newInstance("test.jaxb");



Create an Unmarshaller object.

import javax.xml.bind.Unmarshaller;
Unmarshaller unmarshaller = jc.createUnmarshaller();


Call the unmarshal method

unmarshaller.setValidating(true);// validate source data against schema
Collection collection= (Collection)unmarshaller.unmarshal(new File( "books.xml"));

Here the Collection is the base xml element not java.util.Collection

Use the get methods in the schema-derived classes to access the XML data.


you can unmarshal XML data from other input sources such as an InputStream object, a URL, or a DOM node. You can even unmarshal transformed XML data.

JAXB also allows you to access XML data without having to unmarshal it. One of the classes generated from a schema, ObjectFactory, contains methods to generate objects for each of the schema-derived interfaces and classes. For example, ObjectFactory class that has methods such as createElement_Name to create a object corresponding to Node in the XML document. You can use these methods to create a tree of content objects without doing any unmarshalling. All your program needs is access to the ObjectFactory class that's in the package for the pertinent schema. Then you can use the appropriate methods in the ObjectFactory class to create the objects you need. After you create the objects, you need to provide their content. To do that, you use the set methods in the objects.

(2) Write XML from Classes
you would:

* Bind the schema for the XML document (if it isn't already bound - same as above)
* Create the content tree.

ObjectFactory objFactory = new ObjectFactory();
Collection collection=(Collection) objFactory.createCollection(); // collection is element name
Collection.BooksType booksType = objFactory.createCollectionTypeBooksType(); // books is element name ( acts as a conatiner )
BookType book = objFactory.createBookType(); // its a element name

Then use set methods in the created objects to specify data values. For example:

book.setItemId("307");
book.setName("JAXB today and beyond");


* Marshal the content tree into the XML document.

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
// step 1 - create context
JAXBContext jaxbContext = JAXBContext.newInstance("test.jaxb");
// step 2 - create marshaller
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,new Boolean(true));
// step 3 - call marshal
marshaller.marshal(collection,new FileOutputStream("jaxbOutput.xml"));


Notice that validation is not performed as part of the marshalling operation. In other words, unlike the case for unmarshalling, there is no setValidating method for marshalling. Instead, when marshalling data, you use the Validator class that is a part of the binding framework to validate a content tree against a schema. For example:


import javax.xml.bind.Validator;

Validator validator = jaxbContext.createValidator();
validator.validate(collection))


(3) Updating XML document



References
http://java.sun.com/developer/technicalArticles/WebServices/jaxb/
http://wiki.java.net/bin/view/Javawsxml/JaxbArticles

No comments: