1 / 17

JAXB

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.

eric-ayala
Download Presentation

JAXB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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/webservices/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, 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

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

  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 <students> • 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 "xsd:NCName" 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. • 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.

More Related