Thursday, February 23, 2012

Java Architecture for XML Binding (JAXB)

Overview:
 - Java Architecture for XML Binding (JAXB) is a Java standard that defines how Java objects are converted to/from XML (specified using a standard set of mappings).


 - It defines a programmer API for reading and writing Java objects to / from XML documents and a service provider which / from from XML documents allows the selection of the JAXB implementation

 - It makes reading  and writing of XML via Java very easy.

 - It allows Java developers to access and process XML data without having to know XML or XML processing


 - It is used heavily by JAX-WS

 

 - It provides ways to generate XML content from a Java representation , to generate a Java representation from XML file , to generate XML schema from Java Objects

 - The JAXBContext class provides the client's entry point to the JAXB API. It provides an abstraction for managing the XML/Java binding information necessary to implement the JAXB binding framework operations: unmarshal, marshal and validate.


- A client application obtains new instances of this class via the newInstance(contextPath) method.            
          JAXBContext jc = JAXBContext.newInstance( "customer.class" );
The contextPath contains a list of Java package names that contain schema derived interfaces




Marshalling :

The Marshaller class provides the client application the ability to convert a Java content tree back into XML data. 


Here is a simple example that unmarshals an XML document and then marshals it back out:

          JAXBContext jc = JAXBContext.newInstance( "Customer.class" );
          Unmarshaller u = jc.createUnmarshaller(); 
          Customer customer = (Customer)u.unmarshal( new File( "test.xml" ) );
          Marshaller m = jc.createMarshaller(); m.marshal( customer, System.out );


Unmarshalling
:

The Unmarshaller class provides the client application the ability to convert XML data into a tree of Java content objects. 


For example: 
       JAXBContext jc = JAXBContext.newInstance( "customer.class" ); 
       Unmarshaller u = jc.createUnmarshaller();
       Customer customer = (Customer)u.unmarshal( new File( "test.xml" ) ); // ok                                                                                                            
                                                                                          to be continued...

References:

http://docs.oracle.com/javase/6/docs/api/javax/xml/bind/JAXBContext.html
http://stackoverflow.com/questions/5964996/convert-java-object-to-xml
http://ooxs-be.goracer.nl/EN/java/Java%20and%20XML%20Binding.html

Tutorial:
https://github.com/abdulwaheed18/JAXB-Basic-Tutorial

2 comments:

How TOPT Works: Generating OTPs Without Internet Connection

Introduction Have you ever wondered how authentication apps like RSA Authenticator generate One-Time Passwords (OTPs) without requiring an i...