Using JAXB - PowerPoint PPT Presentation

About This Presentation
Title:

Using JAXB

Description:

Title: Using JAXB Author: mm6 Last modified by: mm6 Created Date: 11/19/2003 12:01:40 AM Document presentation format: On-screen Show Company: CMU Other titles – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 16
Provided by: mm6
Category:
Tags: jaxb | import | using

less

Transcript and Presenter's Notes

Title: Using JAXB


1
Using JAXB
  • Revisited

2
An XSDL Document for an ItemList
  • lt?xml version"1.0" encoding"utf-8"?gt
  • ltxsdschema xmlnsxsd'http//www.w3.org/2001/XMLS
    chema'gt
  • ltxsdelement name"itemList"gt
  • ltxsdcomplexTypegt
  • ltxsdsequencegt
  • ltxsdelement ref"item"
  • minOccurs"0"
    maxOccurs"3"/gt
  • lt/xsdsequencegt
  • lt/xsdcomplexTypegt
  • lt/xsdelementgt

3
ltxsdelement name"item"gt ltxsdcomplexTypegt
ltxsdsequencegt ltxsdelement
ref"name"/gt ltxsdelement
ref"quantity"/gt lt/xsdsequencegt
lt/xsdcomplexTypegt lt/xsdelementgt ltxsdelement
name"name" type"xsdstring"/gt ltxsdelement
name"quantity" type"xsdshort"/gt lt/xsdschemagt
4
A Document Instance
  • lt?xml version"1.0" encoding"utf-8"?gt
  • ltitemList
  • xmlnsxsi'http//www.w3.org/2001/XMLSchema-inst
    ance'
  • xsinoNamespaceSchemaLocation"itemList.xsd"gt
  • ltitemgt
  • ltnamegtpenlt/namegt
  • ltquantitygt5lt/quantitygt
  • lt/itemgt
  • ltitemgt
  • ltnamegteraserlt/namegt
  • ltquantitygt7lt/quantitygt
  • lt/itemgt
  • ltitemgt
  • ltnamegtstaplerlt/namegt
  • ltquantitygt2lt/quantitygt
  • lt/itemgt
  • lt/itemListgt

5
An Ant Build file
  • lt?xml version"1.0"?gt
  • lt! adapted from Suns jaxb build file ?
  • ltproject basedir"." default"run"gt
  • ltpath id"classpath"gt
  • ltfileset dir"D\jwsdp-1.2\jaxb\lib"
    includes".jar"/gt
  • ltfileset dir"D\jwsdp-1.2\common\lib"
    includes".jar"/gt
  • ltfileset dir"D\jwsdp-1.2\jaxp\lib\endors
    ed" includes".jar"/gt
  • ltfileset dir"D\jwsdp-1.2\jwsdp-shared\li
    b" includes".jar"/gt
  • ltfileset dir"D\jwsdp-1.2\jaxp\lib"
    includes".jar"/gt
  • ltpathelement location"."/gt
  • lt/pathgt

6
lttaskdef name"xjc" classname"com.sun.tools.
xjc.XJCTask"gt ltclasspath
refid"classpath" /gt lt/taskdefgt lt!--
generate Java source files --gt lttarget
name"generate"gt lt!-- generate the
Java content classes from the schema --gt
ltecho message"Compiling the schema..."/gt
ltxjc schema"itemList.xsd" target"."
package"itemListAPI"/gt lt!-- generate
the javadocs from the content classes --gt
ltmkdir dir"docs/api"/gt ltjavadoc
packagenames"itemListAPI"
sourcepath"."
destdir"docs/api"
windowtitle"Generated Interfaces for
itemList.xsd"gt ltclasspath
refid"classpath" /gt lt/javadocgt
7
lt!-- compile all of the java sources --gt
ltecho message"Compiling the java source
files..."/gt ltjavac srcdir"." destdir"."
debug"on"gt ltclasspath
refid"classpath" /gt lt/javacgt
lt/targetgt lttarget name"JustJava"gt
lt!-- compile all of the java sources --gt
ltecho message"Compiling the java source
files..."/gt ltjavac srcdir"." destdir"."
debug"on"gt ltclasspath
refid"classpath" /gt lt/javacgt
lt/targetgt
8
lttarget name"JustJavaRun"gt ltecho
message"java runtime"/gt ltjava
classname"Main" fork"true"gt
ltclasspath refid"classpath" /gt lt/javagt
lt/targetgt lttarget name"run"
depends"generate"gt ltecho
message"Running the sample application..."/gt
ltjava classname"Main" fork"true"gt
ltclasspath refid"classpath" /gt
lt/javagt lt/targetgt lt/projectgt
9
Running the build
  • D\McCarthy\www\95-733\examples\struts1\src\VOgtant
    generate
  • Buildfile build.xml
  • generate
  • echo Compiling the schema...
  • xjc Compiling file/D/McCarthy/www/95-733
    /examples/struts1/src/VO/itemL
  • ist.xsd
  • xjc Writing output to D\McCarthy\www\95-7
    33\examples\struts1\src\VO
  • javadoc Generating Javadoc
  • javadoc Javadoc execution
  • javadoc Loading source files for package
    itemListAPI...
  • javadoc Constructing Javadoc information...
  • javadoc Standard Doclet version 1.4.2
  • javadoc Building tree for all the packages
    and classes...
  • javadoc Building index for all the packages
    and classes...
  • javadoc Building index for all classes...
  • echo Compiling the java source files...
  • javac Compiling 42 source files to
    D\McCarthy\www\95-733\examples\struts1
  • \src\VO

10
Viewing the docs
11
Write the client
  • import java.io.File
  • import java.io.IOException
  • import java.math.BigDecimal
  • import javax.xml.bind.JAXBContext
  • import javax.xml.bind.JAXBException
  • import javax.xml.bind.Marshaller
  • import javax.xml.bind.UnmarshalException
  • import javax.xml.bind.Unmarshaller
  • import javax.xml.bind.ValidationEvent
  • import javax.xml.bind.util.ValidationEventCollecto
    r
  • // import java content classes generated by
    binding compiler
  • import itemListAPI.
  • public class Main
  • // This sample application demonstrates how
    to enable validation during

12
public static void main( String args )
try // create a JAXBContext
capable of handling classes generated into
// the itemListAPI package
JAXBContext jc JAXBContext.newInstance(
"itemListAPI" ) //
create an Unmarshaller Unmarshaller u
jc.createUnmarshaller() // enable
validation u.setValidating( true )

13
// in this example, we will allow the
Unmarshaller's default //
ValidationEventHandler to receive notification of
warnings // and errors which will be
sent to System.out. The default //
ValidationEventHandler will cause the unmarshal
operation // to fail with an
UnmarshalException after encountering the
// first error or fatal error.
// unmarshal an invalid itemList
instance document into a tree of Java
// content objects composed of classes from the
itemListAPI package. ItemList
itemList (ItemList)u.unmarshal(
new File( "itemList.xml" ) )
java.util.List list
itemList.getItem()
System.out.println("The length of the list is "
list.size())
14
int n list.size()
for(int k 0 k lt n k)
ItemType it (ItemType)list.get(k)
String st it.getName()
System.out.println(st)
catch( UnmarshalException ue )
// The JAXB specification does not
mandate how the JAXB provider // must
behave when attempting to unmarshal invalid XML
data. In // those cases, the JAXB
provider is allowed to terminate the
// call to unmarshal with an UnmarshalException.
System.out.println( "Caught
UnmarshalException" ) catch(
JAXBException je )
je.printStackTrace()
15
Running the Client
  • D\McCarthy\www\95-733\examples\struts1\src\VOgtant
    JustJavaRun
  • Buildfile build.xml
  • JustJavaRun
  • echo java runtime
  • java The length of the list is 3
  • java pen
  • java eraser
  • java stapler
  • BUILD SUCCESSFUL
  • Total time 2 seconds
Write a Comment
User Comments (0)
About PowerShow.com