Date post: | 28-May-2015 |
Category: |
Technology |
Upload: | srinivasanjayakumar |
View: | 2,584 times |
Download: | 2 times |
JAXBJAXB
Prepared By
Srinivasan Jayakumar
JAXBJAXBJAXB is Java Architecture for XML BindingSAX and DOM are generic XML parsers
◦ They will parse any well-structured XMLJAXB creates a parser that is specific to
your DTD◦ A JAXB parser will parse only valid XML (as
defined by your DTD)DOM and JAXB both produce a tree in
memory◦ DOM produces a generic tree; everything is
a Node◦ JAXB produces a tree of Objects with
names and attributes as described by your DTD
Major Features of JAXBMajor Features of JAXB Ease of development Typed access to XML content Leverage Java SE 5 features 100% XML Schema support Java classes to XML Schema binding Data binding for Java API for XML Web Services
(JAX-WS) Schema evolution Provide optional on-demand validation of XML
documents Hides the complexity of DOM/SAX APIs Is portable (A JAXB application should be able
to utilize different JAXB implementations by just regenerating the schema classes and doesn't require change to the actual application code.)
Architecture of JAXBArchitecture of JAXB
XML Schema
Binding Customizatio
ns
XML Input document
XML Output
document
Application Code
Interface and
Object factory
PackageJava.xml
.bind
Implementation Classes
Implementation of
Java.xml.bind
Binding Compile
r
ArchitectureArchitecture
XML Schema : An XML schema uses XML syntax to describe the relationships among elements, attributes and entities in an XML document. The purpose of an XML schema is to define a class of XML documents that must adhere to a particular set of structural rules and data constraints.
Binding Customizations : By default, the JAXB binding compiler binds Java classes and packages to a source XML schema based on rules. In most cases, the default binding rules are sufficient to generate a robust set of schema-derived classes from a wide range of schemas.
ArchitectureArchitecture
Binding Compiler : The JAXB binding compiler is the core of the JAXB processing model. Its function is to transform, or bind, a source XML schema to a set of JAXB content classes in the Java programming language
Implementation of javax.xml.bind : The JAXB binding framework implementation is a runtime API that provides interfaces for unmarshalling, marshalling, and validating XML content in a Java application. The binding framework comprises interfaces in the javax.xml.bind package.
ArchitectureArchitecture
Schema-Derived Classes: These are the schema-derived classes generated by the binding JAXB compiler. The specific classes will vary depending on the input schema
Java Application : In the context of JAXB, a Java application is a client application that uses the JAXB binding framework to unmarshal XML data, validate and modify Java content objects, and marshal Java content back to XML data.
ArchitectureArchitecture
XML Input Documents: XML content that is unmarshalled as input to the JAXB binding framework -- that is, an XML instance document, from which a Java representation in the form of a content tree is generated
XML Output Documents : XML content that is marshalled out to an XML document. In JAXB, marshalling involves parsing an XML content object tree and writing out an XML document that is an accurate representation of the original XML document, and is valid with respect the source schema.
JAXB Binding ProcessJAXB Binding Process
Schema
Document
Java Mapped Classes
Objects
binding
Unmarshal
Marshal
validate
followsInstance of
Binding ProcessBinding Process
Generate classes: An XML schema is used as input to the JAXB binding compiler to generate JAXB classes based on that schema
Compile classes : All of the generated classes, source files, and application code must be compiled
Unmarshal:: XML documents written according to the constraints in the source schema are unmarshalled by the JAXB binding framework. Note that JAXB also supports unmarshalling XML data from sources other than files/documents, such as DOM nodes, string buffers, SAX Sources, and so forth
Binding ProcessBinding Process
Generate content tree: The unmarshalling process generates a content tree of data objects instantiated from the generated JAXB classes; this content tree represents the structure and content of the source XML documents
Validate (optional) : The unmarshalling process optionally involves validation of the source XML documents before generating the content tree. Note that if you modify the content tree in Step 6, below, you can also use the JAXB Validate operation to validate the changes before marshalling the content back to an XML document
Binding ProcessBinding Process
Process content : The client application can modify the XML data represented by the Java content tree by means of interfaces generated by the binding compiler
Marshal: : The processed content tree is marshalled out to one or more XML output documents. The content may be validated before marshalling
MarshallingMarshalling
Marshalling provides a client application the ability to convert a JAXB-derived Java object tree back into XML data. By default, the Marshaller uses UTF-8 encoding when generating XML data. Client applications are not required to validate the Java content tree before marshalling. There is also no requirement that the Java content tree be valid with respect to its original schema to marshal it back into XML data`
import javax.xml.bind.JAXBContext;import javax.xml.bind.Marshaller;import com.test.*;
A JAXBContext instance is created for handling classes generated in package com.testJAXBContext jc = JAXBContext.newInstance( "com.test" );A Marshaller instance is created, and the updated XML content is marshalled tosystem.out. The setProperty API is used to specify output encoding; in this caseformatted (human readable) XML format.Marshaller m = jc.createMarshaller();m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);m.marshal( po, System.out );
UnmarshallingUnmarshalling
Unmarshalling provides a client application the ability to convert XML data into JAXB-derived Java objects
import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBException;import javax.xml.bind.UnmarshalException;import javax.xml.bind.Unmarshaller;import javax.xml.bind.ValidationEvent;import com.test.*;JAXBContext jc = JAXBContext.newInstance( "com.test" );try{Unmarshaller u = jc.createUnmarshaller();PurchaseOrder po = (PurchaseOrder)u.unmarshal( new FileInputStream("po.xml"));} catch( UnmarshalException ue ) {System.out.println( "Caught UnmarshalException" );} catch( JAXBException je ) {je.printStackTrace();} catch( IOException ioe ) {ioe.printStackTrace();
ValidationValidation
It is the process of verifying that an XML document meets all the constraints expressed in the schema. JAXB 1.0 provided validation at unmarshal time and also enabled on-demand validation on a JAXB content tree. JAXB 2.0 only allows validation at unmarshal and marshal time.
import javax.xml.bind.Validator;Validator validator = jaxbContext.createValidator();validator.validate(po));
JABX plug-in,jarsJABX plug-in,jarsPlug-in :The following plug-in and jars are the pre-requisites for JAXB tool.Step1: Down load the plugins,jars with respect to eclipse 1.0 or 1.1 from the following JAXB link.https://jaxb-workshop.dev.java.net/plugins/eclipse/xjc-plugin.htmlStep2: copy the plugins content to respective plugins folder of your eclipse.Eg:C:\Program Files\Eclipse\pluginsStep3:Then open your workspace using eclipse and right click on XSD(XML Schema definition) fileeg:NewXMLSchema1.xsd
JABX plug-in,jarsJABX plug-in,jars
Step4 : Enter the package name and click on change button , save the files in your Desktop.Eg: D:\Srini\ExJAXB
JABX plug-in,jarsJABX plug-in,jars
Step5 : click on next button and select the check box“Allow vendor extensions” ,click finish button. It will generate the Java classes from XSD file.
Marshalling ExampleMarshalling ExampleMain.java ://declares imports for three standard Java classes plus four JAXB binding frameworkclasses and com.test package:import java.io.FileInputStream;import java.io.IOException;import java.math.BigDecimal;import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBException;import javax.xml.bind.Marshaller;import javax.xml.bind.Unmarshaller;import com.test.*;public class Main{//A JAXBContext instance is created for handling classes generated in packagecom.test(it contain Java classes generated in step5 of Slide 15).JAXBContext jc = JAXBContext.newInstance( "com.test" );
Marshalling ExampleMarshalling Example//An Unmarshaller instance is created, and po.xml is unmarshalled.Unmarshaller u = jc.createUnmarshaller();PurchaseOrder po = (PurchaseOrder)u.unmarshal( new FileInputStream( "po.xml" ) );//set methods are used to modify information in the address branch of the content tree.USAddress address = po.getBillTo();address.setName( "John Bob" );address.setStreet( "242 Main Street" );address.setCity( "Beverly Hills" );address.setState( "CA" );address.setZip( new BigDecimal( "90210" ) );
Marshalling ExampleMarshalling Example//A Marshaller instance is created, and the updated XML content is marshalled tosystem.out.The setProperty API is used to specify output encoding; in this caseformatted (human readable) XML format.Marshaller m = jc.createMarshaller();m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);m.marshal( po, System.out );
AdvantagesAdvantages
JAXB requires a DTD Using JAXB ensures the validity of your XML
A JAXB parser is actually faster than a generic SAX parser
A tree created by JAXB is smaller than a DOM tree
It’s much easier to use a JAXB tree for application-specific code
You can modify the tree and save it as XML
DisadvantagesDisadvantages
JAXB requires a DTDHence, you cannot use JAXB to process generic XML (for example, if you are writing an XML editor or other tool)
You must do additional work up front to tell JAXB what kind of tree you want it to constructBut this more than pays for itself by simplifying your application
Thank YouThank You