1 / 15

Using JAXB

Using JAXB. Revisited. An 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"

chelsi
Download Presentation

Using 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. Using JAXB Revisited

  2. An 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>

  4. A 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>

  5. An 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>

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

More Related