Transcript Document

JAXB
Java Architecture for XML Bindings
What is JAXB?
• JAXB defines the behavior of a standard set of tools and interfaces that
automatically generate java class files from XML schema
• JAXB is a framework or architecture, not an implementation.
• Sun provides a reference implementation of JAXB with the Web
Services Developers kit, available as a separate download
http://java.sun.com/webservices/downloads/webservicespack.html
JAXB vs. DOM and SAX
• JAXB is a higher level construct than DOM or SAX
– DOM represents XML documents as generic trees
– SAX represents XML documents as generic event streams
– JAXB represents XML documents as Java classes with properties
that are specific to the particular XML document
• E.g. book.xml becomes Book.java with getTitle, setTitle, etc.
• JAXB thus requires almost no knowledge of XML to be
able to programmatically process XML documents!
High-level comparison
• Before diving into details of JAXB, it’s good to see a
bird’s-eye-view of the difference between JAXB and SAX
and/or DOM-like parsers
• Study the books/ examples under the examples/jaxb
directory on the course website
JAXB steps
•
We start by assuming that you have a
valid installation of java web services
developers pack version 3. We cover
these installation details later
•
Using JAXB then requires several
steps:
1.
2.
3.
4.
Run the binding compiler on the
schema file to automagically produce
the appropriate java class files
Compile the java class files (ant tool
helps here)
Study the autogenerated api to learn
what java types have been created
Create a program that unmarshals an
xml document into these elementary
data structures
Running binding compiler
• <install_dir>/jaxb/bin/xjc.sh -p test.jaxb books.xsd -d work
–
–
–
–
xjc.sh : executes binding compiler
-p test.jaxb : place resulting class files in package test.jaxb
books.xsd : run compiler on schema books.xsd
-d work : place resulting files in directory called work/
• Note that this creates a huge number of files that together represent the
content of the books.xsd schema as a set of Java classes
• It is not necessary to know all of these classes. We’ll study them only
at a high level so we can understand how to use them
Example: students.xsd
Generated interfaces
•
xjc.sh -p test.lottery students.xsd
• This generates the following interfaces
– test/lottery/ObjectFactory.java
• Contains methods for generating instances of the interfaces
– test/lottery/Students.java
• Represents the root node <students>
– test/lottery/StudentsType.java
• Represents the unnamed type of each student object
Generated implementations
• Each interface is implemented in the impl directory
– test/lottery/impl/StudentsImpl.java
• Vendor-specific implementation of the Students inteface
– test/lottery/impl/StudentsTypeImpl.java
• Vendor-specific implementation of the StudentsType Interface
Compilation
• Next, the generated classes must be compiled:
– javac students/*.java students/impl/*.java
• CLASSPATH requires many jar files:
– jaxb/lib/*.jar
– jwsdp-shared/lib/*.jar
– jaxp/lib/**/*.jar
• Note: an ant buildfile (like a java makefile) makes this
much easier. More on this later
Generated docs
• Java API docs for these classes are generated in
– students/docs/api/*.html
• After bindings are generated, one usually works directly
through these API docs to learn how to access/manipulate
the XML data.
Sample Programs
Sample Programs
• Easiest way to learn is to cover certain generic sample cases. These are
all on the course website under cspp53025/examples/jaxb
• Summary of examples:
– student/
• Use JAXB to read an xml document composed of a single student complex
type
– student/
• Same, but for an xml document composed of a sequence of such student types
of indefinite length
– purchaseOrder/
• Another read example, but for a more complex schema
Sample programs, cont
• Course examples, cont
– create-marshal
• Purchase-order example modified to create in memory and write to
XML
– modify-marshal
• Purchase-order example modified to read XML, change it and write
back to XML
• Study these examples!
Some additional JAXB details
Binding Data Types
• Default java datatype bindings can be found at:
http://java.sun.com/webservices/docs/1.3/tutorial/doc/JAXBWorks5.html
• These defaults can be changed if required for an
application
• Also, name binding are fairly standard changes of names to
things acceptable in java programming language
• See other binding rules on subsequent pages
Default binding rules summary
•
The JAXB binding model follows the default binding rules summarized below:
•
Bind the following to Java package:
–
•
Bind the following XML Schema components to Java content interface:
–
–
•
A global element declaration to a Element interface.
Local element declaration that can be inserted into a general content list.
Bind to Java property:
–
–
•
A named simple type definition with a basetype that derives from "xsd:NCName" and has enumeration facets.
Bind the following XML Schema components to a Java Element interface:
–
–
•
Named complex type
Anonymous inlined type definition of an element declaration
Bind to typesafe enum class:
–
•
XML Namespace URI
Attribute use
Particle with a term that is an element reference or local element declaration.
Bind model group with a repeating occurrence and complex type definitions with mixed {content type} to:
–
A general content property; a List content-property that holds Java instances representing element information items and character
data items.