Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Using JAXB Revisited. 2 An XSDL Document for an ItemList <xsd:element ref="item" minOccurs="0" maxOccurs="3"/>

Similar presentations


Presentation on theme: "1 Using JAXB Revisited. 2 An XSDL Document for an ItemList <xsd:element ref="item" minOccurs="0" maxOccurs="3"/>"— Presentation transcript:

1 1 Using JAXB Revisited

2 2 An XSDL Document for an ItemList <xsd:element ref="item" minOccurs="0" maxOccurs="3"/>

3 3

4 4 A Document Instance <itemList xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation="itemList.xsd"> pen 5 eraser 7 stapler 2

5 5 An Ant Build file <!– adapted from Sun’s jaxb build file 

6 6 <javadoc packagenames="itemListAPI" sourcepath="." destdir="docs/api" windowtitle="Generated Interfaces for itemList.xsd">

7 7

8 8

9 9 Running the build D:\McCarthy\www\95-733\examples\struts1\src\VO>ant 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-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\VO BUILD SUCCESSFUL

10 10 Viewing the docs

11 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.ValidationEventCollector; // import java content classes generated by binding compiler import itemListAPI.*; public class Main { // This sample application demonstrates how to enable validation during // the unmarshal operations.

12 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 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 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(); }

15 15 Running the Client D:\McCarthy\www\95-733\examples\struts1\src\VO>ant 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


Download ppt "1 Using JAXB Revisited. 2 An XSDL Document for an ItemList <xsd:element ref="item" minOccurs="0" maxOccurs="3"/>"

Similar presentations


Ads by Google