Unformatted text preview:

Using JAXBAn XSDL Document for an ItemListSlide 3A Document InstanceAn Ant Build fileSlide 6Slide 7Slide 8Running the buildViewing the docsWrite the clientSlide 12Slide 13Slide 14Running the Client1Using JAXBRevisited2An XSDL Document for an ItemList<?xml version="1.0" encoding="utf-8"?><xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema'> <xsd:element name="itemList"> <xsd:complexType> <xsd:sequence> <xsd:element ref="item" minOccurs="0" maxOccurs="3"/> </xsd:sequence> </xsd:complexType> </xsd:element>3<xsd:element name="item"> <xsd:complexType> <xsd:sequence> <xsd:element ref="name"/> <xsd:element ref="quantity"/> </xsd:sequence> </xsd:complexType></xsd:element> <xsd:element name="name" type="xsd:string"/> <xsd:element name="quantity" type="xsd:short"/></xsd:schema>4A Document Instance<?xml version="1.0" encoding="utf-8"?><itemList xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation="itemList.xsd"> <item> <name>pen</name> <quantity>5</quantity> </item> <item> <name>eraser</name> <quantity>7</quantity> </item> <item> <name>stapler</name> <quantity>2</quantity> </item></itemList>5An Ant Build file<?xml version="1.0"?><!– adapted from Sun’s jaxb build file <project basedir="." default="run"> <path id="classpath"> <fileset dir="D:\jwsdp-1.2\jaxb\lib" includes="*.jar"/> <fileset dir="D:\jwsdp-1.2\common\lib" includes="*.jar"/> <fileset dir="D:\jwsdp-1.2\jaxp\lib\endorsed" includes="*.jar"/> <fileset dir="D:\jwsdp-1.2\jwsdp-shared\lib" includes="*.jar"/> <fileset dir="D:\jwsdp-1.2\jaxp\lib" includes="*.jar"/> <pathelement location="."/> </path>6 <taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask"> <classpath refid="classpath" /> </taskdef> <!-- generate Java source files --> <target name="generate"> <!-- generate the Java content classes from the schema --> <echo message="Compiling the schema..."/> <xjc schema="itemList.xsd" target="." package="itemListAPI"/> <!-- generate the javadocs from the content classes --> <mkdir dir="docs/api"/> <javadoc packagenames="itemListAPI" sourcepath="." destdir="docs/api" windowtitle="Generated Interfaces for itemList.xsd"> <classpath refid="classpath" /> </javadoc>7 <!-- compile all of the java sources --> <echo message="Compiling the java source files..."/> <javac srcdir="." destdir="." debug="on"> <classpath refid="classpath" /> </javac> </target> <target name="JustJava"> <!-- compile all of the java sources --> <echo message="Compiling the java source files..."/> <javac srcdir="." destdir="." debug="on"> <classpath refid="classpath" /> </javac> </target>8 <target name="JustJavaRun"> <echo message="java runtime"/> <java classname="Main" fork="true"> <classpath refid="classpath" /> </java> </target> <target name="run" depends="generate"> <echo message="Running the sample application..."/> <java classname="Main" fork="true"> <classpath refid="classpath" /> </java> </target></project>9Running the buildD:\McCarthy\www\95-733\examples\struts1\src\VO>ant generateBuildfile: build.xmlgenerate: [echo] Compiling the schema... [xjc] Compiling file:/D:/McCarthy/www/95-733/examples/struts1/src/VO/itemList.xsd [xjc] Writing output to D:\McCarthy\www\95-733\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\VOBUILD SUCCESSFUL10Viewing the docs11Write the clientimport 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.ValidationEventCollector;// import java content classes generated by binding compilerimport itemListAPI.*; public class Main { // This sample application demonstrates how to enable validation during // the unmarshal operations.12public 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 < 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(); } }}15Running the ClientD:\McCarthy\www\95-733\examples\struts1\src\VO>ant JustJavaRunBuildfile: build.xmlJustJavaRun: [echo] java runtime [java] The length of the list is 3 [java] pen [java] eraser [java] staplerBUILD SUCCESSFULTotal time: 2


View Full Document

CMU ISM 95733 - UsingJAXB

Download UsingJAXB
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view UsingJAXB and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view UsingJAXB 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?