Published on

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. JAXB Prepared By Srinivasan Jayakumar
  2. 2. JAXB <ul><li>JAXB is Java Architecture for XML Binding </li></ul><ul><li>SAX and DOM are generic XML parsers </li></ul><ul><ul><li>They will parse any well-structured XML </li></ul></ul><ul><li>JAXB creates a parser that is specific to your DTD </li></ul><ul><ul><li>A JAXB parser will parse only valid XML (as defined by your DTD) </li></ul></ul><ul><li>DOM and JAXB both produce a tree in memory </li></ul><ul><ul><li>DOM produces a generic tree; everything is a Node </li></ul></ul><ul><ul><li>JAXB produces a tree of Objects with names and attributes as described by your DTD </li></ul></ul>
  3. 3. Major Features of JAXB <ul><li>Ease of development </li></ul><ul><li>Typed access to XML content </li></ul><ul><li>Leverage Java SE 5 features </li></ul><ul><li>100% XML Schema support </li></ul><ul><li>Java classes to XML Schema binding </li></ul><ul><li>Data binding for Java API for XML Web Services (JAX-WS) </li></ul><ul><li>Schema evolution </li></ul><ul><li>Provide optional on-demand validation of XML documents </li></ul><ul><li>Hides the complexity of DOM/SAX APIs </li></ul><ul><li>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.) </li></ul>
  4. 4. Architecture of JAXB XML Schema Binding Customizations XML Input document XML Output document Application Code Interface and Object factory Package Java.xml. bind Implementation Classes Implementation of Java.xml.bind Binding Compiler
  5. 5. Architecture 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.
  6. 6. Architecture 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.
  7. 7. Architecture 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 .
  8. 8. Architecture 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.
  9. 9. JAXB Binding Process Schema Document Java Mapped Classes Objects binding Unmarshal Marshal validate follows Instance of
  10. 10. Binding 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
  11. 11. Binding 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
  12. 12. Binding 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
  13. 13. Marshalling 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 `
  14. 14. 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.test JAXBContext jc = JAXBContext.newInstance( &quot;com.test&quot; ); A Marshaller instance is created, and the updated XML content is marshalled to system.out. The setProperty API is used to specify output encoding; in this case formatted (human readable) XML format. Marshaller m = jc.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); m.marshal( po, System.out );
  15. 15. Unmarshalling Unmarshalling provides a client application the ability to convert XML data into JAXB-derived Java objects
  16. 16. 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( &quot;com.test&quot; ); try{ Unmarshaller u = jc.createUnmarshaller(); PurchaseOrder po = (PurchaseOrder)u.unmarshal( new FileInputStream(&quot;po.xml&quot;)); } catch( UnmarshalException ue ) { System.out.println( &quot;Caught UnmarshalException&quot; ); } catch( JAXBException je ) { je.printStackTrace(); } catch( IOException ioe ) { ioe.printStackTrace();
  17. 17. Validation 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));
  18. 18. JABX plug-in,jars Plug-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.html Step2: copy the plugins content to respective plugins folder of your eclipse. Eg:C:Program FilesEclipseplugins Step3:Then open your workspace using eclipse and right click on XSD (XML Schema definition) file eg:NewXMLSchema1.xsd
  19. 20. JABX plug-in,jars Step4 : Enter the package name and click on change button , save the files in your Desktop. Eg: D:SriniExJAXB
  20. 21. JABX 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.
  21. 22. Marshalling Example Main.java : //declares imports for three standard Java classes plus four JAXB binding framework classes 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 package com.test(it contain Java classes generated in step5 of Slide 15). JAXBContext jc = JAXBContext.newInstance( &quot;com.test&quot; );
  22. 23. Marshalling Example //An Unmarshaller instance is created, and po.xml is unmarshalled. Unmarshaller u = jc.createUnmarshaller(); PurchaseOrder po = (PurchaseOrder)u.unmarshal( new FileInputStream( &quot;po.xml&quot; ) ); //set methods are used to modify information in the address branch of the content tree. USAddress address = po.getBillTo(); address.setName( &quot;John Bob&quot; ); address.setStreet( &quot;242 Main Street&quot; ); address.setCity( &quot;Beverly Hills&quot; ); address.setState( &quot;CA&quot; ); address.setZip( new BigDecimal( &quot;90210&quot; ) );
  23. 24. Marshalling Example //A Marshaller instance is created, and the updated XML content is marshalled to system.out.The setProperty API is used to specify output encoding; in this case formatted (human readable) XML format. Marshaller m = jc.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); m.marshal( po, System.out );
  24. 25. Advantages <ul><li>JAXB requires a DTD </li></ul><ul><li>Using JAXB ensures the validity of your XML </li></ul><ul><li>A JAXB parser is actually faster than a generic SAX parser </li></ul><ul><li>A tree created by JAXB is smaller than a DOM tree </li></ul><ul><li>It’s much easier to use a JAXB tree for application-specific code </li></ul><ul><li>You can modify the tree and save it as XML </li></ul>
  25. 26. Disadvantages <ul><li>JAXB requires a DTD </li></ul><ul><li>Hence, you cannot use JAXB to process generic XML (for example, if you are writing an XML editor or other tool) </li></ul><ul><li>You must do additional work up front to tell JAXB what kind of tree you want it to construct </li></ul><ul><li>But this more than pays for itself by simplifying your application </li></ul>
  26. 27. Thank You