JAXB - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

JAXB

Description:

... as a separate download http://java.sun.com/webservices/downloads/webservicespack. ... Compile the java class files (ant tool helps here) ... – PowerPoint PPT presentation

Number of Views:97
Avg rating:3.0/5.0
Slides: 18
Provided by: peopleCs
Category:
Tags: jaxb | com | download | html | java | sun | tutorial

less

Transcript and Presenter's Notes

Title: JAXB


1
JAXB
  • Java Architecture for XML Bindings

2
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/webserv
    ices/downloads/webservicespack.html

3
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!

4
High-level comparison
  • Before diving into details of JAXB, its good to
    see a birds-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

5
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
  • 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

6
Running binding compiler
  • ltinstall_dirgt/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.
    Well study them only at a high level so we can
    understand how to use them

7
Example students.xsd
8
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 ltstudentsgt
  • test/lottery/StudentsType.java
  • Represents the unnamed type of each student object

9
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

10
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

11
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.

12
Sample Programs
13
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

14
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!

15
Some additional JAXB details
16
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

17
Default binding rules summary
  • The JAXB binding model follows the default
    binding rules summarized below
  • Bind the following to Java package
  • XML Namespace URI
  • Bind the following XML Schema components to Java
    content interface
  • Named complex type
  • Anonymous inlined type definition of an element
    declaration
  • Bind to typesafe enum class
  • A named simple type definition with a basetype
    that derives from "xsdNCName" and has
    enumeration facets.
  • Bind the following XML Schema components to a
    Java Element 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
  • Attribute use
  • Particle with a term that is an element reference
    or local element declaration.
Write a Comment
User Comments (0)
About PowerShow.com