JAXB: Binding between XML Schema and Java Classes Asst. Prof. Dr. Kanda Runapongsa (krunapon@kku.ac.th) Department of Computer Engineering Khon Kaen University Agenda JAXB Architecture Representing XML Content Binding XML Schemas Customizing JAXB Bindings General Usage Instructions Basic Examples Customizing JAXB Bindings Java-toSchema Examples University 1
What is JAXB? JAXB stands fro Java Architecture for XML Binding JAXB provides a fast and convenient way to bind between XML schemas and Java representations JAXB makes it easy for Java developers to incorporate XML data and processing functions in Java applications JAXB Architectural Overview University 2
JAXB Architectural Components (1/2) Schema compiler Binds a source schema to a set of schema derived program elements The binding is described by an XMLbased binding language Schema generator Maps a set of existing program elements to a derived schema The mapping is described by program annotations JAXB Architectural Components (2/2) Binding runtime framework Provides unmarshalling (reading) and marshalling (writing) operations Accessing, manipulating and validating XML content using Schema-derived or Existing program elements University 3
The JAXB Binding Process Steps in the JAXB Binding Process (1/4) 1. Generate classes An XML schema is used as input to the JAXB binding compiler to generate JAXB classes based on that schema 2. Compile classes All of the generated classes, source files, and application code must be compiled University 4
Steps in the JAXB Binding Process (2/4) 3. Unmarshal XML documents written according to the constraints in the source schema are unmarshalled by the JAXB binding framework JAXB also supports unmarshalling XML data from sources other than files/document, such as DOM nodes, string buffers, SAX Sources, and so forth Steps in the JAXB Binding Process (3/4) 4. 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 5. Validate (optional) The unmarshalling process optionally involves validation of the source XML document before generating the content tree University 5
Steps in the JAXB Binding Process (4/4) 6. 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 7. Marshal The processed content tree is marshalled out to one or more XML output documents The content may be validated before marshalling Java Representation of XML Schema JAXB supports the grouping of generated classes in Java packages. A package comprises: A Java class name is derived from the XML element name, or specified by a binding customization. An ObjectFactory class is a factory that is used to return instances of a bound Java class. University 6
Default Data Type Bindings (1/5) XML Schema Type Java Data Type xsd:string xsd:integer xsd:int xsd:long xsd:short java.lang.string java.math.biginteger int long short Default Data Type Bindings (2/5) XML Schema Type Java Data Type xsd:decimal xsd:float xsd:double xsd:boolean xsd:byte java.math.bigdecimal float double boolean byte University 7
Default Data Type Bindings (3/5) XML Schema Type xsd:qname xsd:datetime xsd:base64binary xsd:hexbinary Xsd:unsignedInt Java Data Type javax.xml.namespace.q Name Javax.xml.datatype. XMLGregorianCalendar byte[] byte[] long Default Data Type Bindings (4/5) XML Schema Type xsd:unsignedshort xsdunsignedbyte xsd:time xsd:date xsd:g Java Data Type int short XMLGregorianCalendar XMLGregorianCalendar XMLGregorianCalendar University 8
Default Data Type Bindings (5/5) XML Schema Type xsd:anysimpletype xsd:duration xsd:notation Java Data Type java.lang.object Duration javax.xml.namespace.qname Mapping of Java Classes to XML Data Types (1/3) Java Class java.lang.string java.math.biginteger java.math.bigdecimal java.util.calendar java.util.date XML Data Type xs:string xs:integer xs:decimal xs:datetime xs:datetime University 9
Mapping of Java Classes to XML Data Types (2/3) Java Class javax.xml.namespace. QName java.net.uri java.util.uuid XMLGregorianCalendar XML Data Type xs:qname xs:string xs:string xs:anysimpletype Mapping of Java Classes to XML Data Types (3/3) Java Class Duration javax.lang.object java.awt.image javax.activiation. DataHandler Javax.xml.transform.Sour ce XML Data Type xs:duration xs:anytype xs:base64binary xs:base64binary xs:base64binary University 10
Customizing JAXB Bindings Schema-to-Java Custom JAXB binding declarations also allow you to customize your generated JAXB classes to include Java-specific refinements such as class and pacage name mappings JAXB provides two ways to customize an XML schema As inline annotations in a source XML schema As declaration in an external binding customizations file that is passed to the JAXB binding compiler Java-to-Schema XML schema that is generated from Java objects can be customized with JAXB annotations References Java Web Services Developer Pack 2.0 http://java.sun.com/webservices/downlo ads/webservicespack.html University 11