Java Reference
In-Depth Information
The first difference you'll notice is that JAXB 2.1 generates simple and clean annotated
POJOs (Plain Old Java Objects) during unmarshaling. The annotations on these concrete
classes indicate hints to JAXB for use during marshaling and unmarshaling. We'll examine
the use and meaning of the annotations in the next section; for now, let's look at an example.
Say you have the schema in Example 2-8 .
Example2-8.XML schema to bind to a Java class
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://ns.soacookbook.com/suits"
xmlns:tns="http://ns.soacookbook.com/suits"
elementFormDefault="qualified">
<xs:simpleType name="Suit">
<xs:restriction base="xs:string">
<xs:enumeration value="SPADES" />
<xs:enumeration value="HEARTS" />
<xs:enumeration value="DIAMONDS" />
<xs:enumeration value="CLUBS" />
</xs:restriction>
</xs:simpleType>
</xs:schema>
This schema defines the suits available for playing cards. Example 2-9 shows an XML docu-
ment instance that would validate against that schema.
Example2-9.XML document matching the suit schema
<?xml version="1.0" encoding="UTF-8"?>
<s:suit xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:s='http://ns.soacookbook.com/suits'
xsi:schemaLocation='http://ns.soacookbook.com/suits
Suits.xsd'>HEARTS</s:suit>
This could be added to your SOAP message as the payload document. But if you define a web
service that accepts an instance of a Java Suit enum, you won't be able to compile your ser-
vice class unless you have the Suit enum on your classpath.
So you need to create a Java source that matches the schema. To do this, run XJC from the
command line to generate the Java sources. Here you will change directories in the console to
your project root. You have a directory called work/genthat you want to put your files into.
Using the -d switch specifies the directory into which you want files generated. You can spe-
cify either a single schema filename to perform compilation across only that single schema, or
you can specify a directory and XJC will compile all schemas it finds in that directory:
Search WWH ::




Custom Search