Binding Between XML Schema and Java Classes (JAXB)
Binding Between XML Schema and Java Classes (JAXB)
Binding Between XML Schema and Java Classes (JAXB)
Introduction
Architecture
Binding Process
Schema Validation Framework
#JAXB #Validation
Objectives
• How to convert XML Schema to Java Object
Class?
• How to binding XML document to Java Object
and Java Object to XML document?
– Binding Process
• How to validate XML in Java Application
– Schema Validation Framework
JAXB
Need for JAXB
• To parse XML using DOM API, the developer must be required to
discover and understand the XML structure in the owner’s
meaning
– The developer is required to understand and get more experiences in using
DOM API
– However, the JAXP supporting XPath API has already reduced the
developer‘s burden in programming (but developer must also understand the
XML document’s structure to write Xpath Expression)
• To reduce the programming burden of XML processing, the
framework in automatically generating the mapping is needed
• There are two major approaches to data binding
– One is to deduce a Java data structure from a given XML schema
• If an XML schema is given and developer are required to write a program to read
XML data according to the schema, a data binding tool of the former type is useful
– The other is to deduce an XML structure from a given Java program
• If developers already have their application program and they are requested to
externalize the application data in some XML format, one of the reflection-based
tools is a better choice
JAXB
Introduction
• Is briefly the Java Architecture for XML Binding
• Binds the XML schemas/DTD and Java representations in a fast
and convenient way (e.g. comparing with DOM & SAX).
• Converts XML instance documents into Java content trees
(unmarshaller)
• Converts Java content trees back into XML instance documents
(marshaller).
• Can make it easier to access XML documents from applications
written in the Java programming language.
– To achieve this, binding the schema for the XML document into a set of Java
classes that represents the schema
• The JAXB API
– Defined in the javax.xml.bind package
– Consists of a set of interfaces through which client applications with code
generated from a schema
– Provides a good quality XML data-binding facility for the J2EE/JavaEE
platform.
JAXB
Limitations
• JAXB requires a DTD and a subset of XML
Schemas.
– Hence, it cannot be used to process generic XML, such
as writing an XML editor or other tool.
• Additional work is required to tell JAXB what
kind of tree it should construct to simplify the
application.
• JAXB does not support the legal DTD constructs
such as Internal subsets, NOTATIONs, ENTITY
and ENTITIES, and Enumerated NOTATION
types.
JAXB
Architecture
IBM,
http://pic.dhe.ibm.com/infocenter/wasinfo/v7r0/index.jsp?topic=%2Fcom.ibm.websphere.express.doc%2Finfo
%2Fexp%2Fae%2Fcwbs_jaxb.html
JAXB
Architecture
• Consists of three components:
– Schema compiler
• Binds a source schema to a set of schema derived program
elements using an XML-based binding language.
– Binding language: describes schema binding of Java classes which
enables a developer to override the default binding rules
→ Generates Java classes from a given schema
– Schema generator
• Maps a set of existing program elements to a derived
schema.
– Binding runtime framework
• Provides runtime services such as marshalling (writing),
unmarshalling (reading), and validation to be performed on
the contents classes
JAXB
Binding Process
1. JAXB binding compiler 2.
Compiling
src code
4.
4.1 5. Process
4.2 Content
3.
6. tree
JAXB
Binding Process
• Generate classes
– First, the JAXB binding compiles the XML schema to generate JAXB
classes based on that schema.
• Compile classes
– All the generated classes, sources files/codes should be compiled.
• Unmarshal
– Unmarshal the XML documents with the help of JAXB binding framework.
• Generate content tree
– The unmarshalling process generates tree of data objects which represents
the structure and content of the source XML documents.
• Validate (optional)
– The unmarshalling process validates the source XML documents before
generating the content tree.
• Process content
– The client application modifies the XML data represented by the Java content
tree.
• Marshal
– The processed content tree is marshaled into XML documents which may be
further validated before marshalling.
JAXB
Generating Java Class from XML Schema – Example
JAXB
Generating Java Class from XML Schema – Example
• In the netbeans ide,
– Choose the XML categories, then choose JAXB Binding
– Choose Next
JAXB
Generating Java Class from XML Schema – Example
Click Finish
JAXB
Generating Java Class from XML Schema – Example
JAXB
Generating Java Class from XML Schema with
Console – Example
JAXB
Dynamically Generating Java Class from XML
Schema with Java API – Example
JAXB
Dynamically Generating Java Class from XML
Schema with Java API – Example
JAXB
Dynamically Generating Java Class from XML
Schema with Java API – Example
JAXB
APIs – JAXBContext class
• Provides a client program’s entry point to the JAXB API
• Provides a set of methods for implementing the JAXB Binding framework
operations
Methods Descriptions
-public abstract Marshaller createMarshaller() throws
JAXBException
createMarshaller
-Creates a Marshaller object that converts a Java content tree
into XML data
-public abstract Unmarshaller createUnmarshaller() throws
JAXBException
createUnmarshaller
-Creates a Unmarshaller object that converts XML data into
a Java content tree
-public static JAXBContext newInstance (Class…
classesToBeBound) throws JAXBException
newInstance -Returns a new instance of a JAXBContext class
-May throw IllegalArgumentException if null argument is
passed
JAXB
Unmarshalling
Q&A
Next Lecture
• How to parse XML document to process in
application
– Simple API for XML (SAX)
– Streaming API for XML (StAX)
Appendix
JAXB APIs
• Are implemented in 3 main Java Packages
Packages Descriptions
-Defines abstract classes and interfaces that can be
used in applications
javax.xml.bind -Defines a hierarchy of validation event and
exception classes
-Defines Unmarshaller, Validator, and Marshaller
-Contains a set of utility classes that can be used by a
javax.xml.bind.util client application for handling marshalling,
unmarshalling, and validation events
-Provides default implementations for some of the
javax.xml.bind interfaces.
javax.xml.bind.helper
-Implementations of JAXB can further extend these
classes, and implement the abstract method
JAXB
APIs – JAXBException class
• Is the root exception class for all JAXB Exceptions that
may occur during JAXB operations
Methods Descriptions
-public Throwable getCause()
getCause -Returns the cause of an exception or null if the
cause is unknown or does not exist
-public String getErrorCode()
getErrorCode
-Retrieves a vendor specific error code
-public void printStackTrace (PrintWriter pw)
printStackTrace -Displays the stack trace of an exception to the
PrintWriter object