Runtime Libraries
First, you'll need to download the Java Web Services Developer Pack 2.0, which includes an implementation of JAXB 2.0. Install JWSDP 2.0 in the C:\Sun\jwsdp-2.0, the default installation directory. JAXB 2.0 gets installed in the C:\Sun\jwsdp-2.0\jaxb directory. Add the C:\Sun\jwsdp-2.0\jaxb\bin directory to the PATH variable. Because JAXB 2.0 uses the JDK5.0 feature parameterized types, you'll need to also install JDK 5.0. Add the environment variables JAVA_HOME and JAXB_HOME. JAVA_HOME specifies the directory in which J2SE 5.0 is installed. JAXB_HOME specifies the directory in which JAXB 2.0 is installed. Table 1 lists the JAR files required for JAXB 2.0.
|
JAR File
|
Description
|
|
C:/Sun/jwsdp-2.0/jaxb/lib/jaxb-api.jar
|
JAXB 2.0 API classes
|
|
C:/Sun/jwsdp-2.0/jaxb/lib/jaxb-impl.jar
|
JAXB 2.0 implementation classes
|
|
C:/Sun/jwsdp-2.0/jaxb/lib/jaxb-xjc.jar
|
JAXB 2.0 Compiler classes
|
|
C:/Sun/jwsdp-2.0/jwsdp-shared/lib/activation.jar
|
javax.activation package classes.
|
|
C:/Sun/jwsdp-2.0/sjsxp/lib/jsr173_api.jar
|
StAX API classes
|
|
C:/Sun/jwsdp-2.0/sjsxp/lib/sjsxp.jar
|
StAX classes
|
Table 1. JAXB 2.0 JAR Files
In comparison, the JAXB 1.0 libraries are listed below:
<JWSDP1.6>/jaxb/lib/jaxb-api.jar
<JWSDP1.6>/jaxb/lib/jaxb-impl.jar
<JWSDP1.6>/jaxb/lib/jaxb-libs.jar
<JWSDP1.6>/jaxb/lib/jaxb-xjc.jar
<JWSDP1.6>/jwsdp-shared/lib/namespace.jar
<JWSDP1.6>/jwsdp-shared/lib/jax-qname.jar
<JWSDP1.6>/jwsdp-shared/lib/relaxngDatatype.jar
Compiling an XML Schema
Listing 1 shows an example XML schema that is compiled using the JAXB 2.0 schema binding compiler .xjc.
The schema binding compiler is used to generate Java classes from an XML schema. Run the schema binding compiler with the following command.
>xjc catalog.xsd
This generates Java classes in the generated package:
parsing a schema...
compiling a schema...
generated\ArticleType.java
generated\CatalogType.java
generated\JournalType.java
generated\ObjectFactory.java
As you can see, fewer Java classes are generated. JAXB 2.0 generates a Java value class that corresponds to each top-level complexType. For example, for catalogType, 2.0 generates the Java value class CatalogType.java. CatalogType.java consists of getter and setter methods for each of the elements and attributes in the complexType. As the catalog element may have more than one journal element, the return type of getJournal() mehod is java.util.List<JournalType>.
Similarly, 2.0 generates the Java value class ArticleType.java, which corresponds to complexType articleType. It also generates JournalType.java, which further corresponds to complexType journalType. Also generated is the ObjectFactory.java class, consisting of create methods which create Java value class objects for ArticleType.java, CatalogType.java, and JournalType.java. The factory class also contains a create method corresponding to each of the top-level elements.
In contrast, JAXB 1.0 generates an interface and an implementation class for each complexType and top-level element. When the XML schema compiled in Listing 1 is compiled by JAXB 1.0, it generates all the classes shown in Listing 2significantly more than what is generated by JAXB 2.0.
Support for All XML Schema Constructs
JAXB 1.0 does not support key XML Schema components like any, anyAttribute, key, keyref, and unique. It also does not support attributes like complexType.abstract, element.abstract, element.substitutionGroup, xsi:type, complexType.block, complexType.final, element.block, element.final, schema.blockDefault, and schema.finalDefault. In version 2.0, support has been added for all of these schema constructs.
To see how it works, first define an XML document consisting of the xs:any and xs:anyAttribute schema constructs:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="catalog" type="catalogType"/>
<xsd:complexType name="catalogType">
<xsd:sequence>
<xsd:any/>
</xsd:sequence>
<xsd:attribute name="journal" type="xsd:string"/>
<xsd:attribute name="publisher" type="xsd:string"/>
<xsd:anyAttribute/>
</xsd:complexType>
</xsd:schema>
Next, compile the XML document using schema compiler xjc:
>xjc catalog.xsd
The output from the schema compilation is shown below:
parsing a schema...
compiling a schema...
generated\CatalogType.java
generated\ObjectFactory.java
In the schema-derived class CatalogType.java, the xs:any element is mapped with the @XmlAnyElement annotation and the xs:anyAttribute is mapped with the @XmlAnyAttribute annotation. JAXB 2.0 provides support for all the XML schema constructs in the schema, allowing you to a wider range of schemas and documents.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|