O'Reilly logo

Java SOA Cookbook by Eben Hewitt

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Generating Java Classes from Schema

Problem

You have taken care to create solid XML schemas representing your data types and now you want to generate Java sources from them.

Solution

Use the XML/Java Compiler XJC tool that comes with Java to compile your schemas into Java classes. If you’re using Ant, use Sun’s <xjc> wrapper task.

Note

Sun does not ship Ant tasks with Java SE, and the task for XJC is no exception. You can find it in the glassfish/lib directory webservices-tools.jar. You may also need webservices-rt.jar on the classpath as well.

Working with JAXB

The purpose of JAXB is to provide a convenient and easy way to bind an XML schema to its corresponding representation in a set of Java classes. It has two primary functions:

Marshaling

Creating XML document instances from Java objects

Unmarshaling

Creating Java objects from XML document instances

JAXB used to be a bit more difficult to use than it is now. The 1.0 version came with the Web Services Developer pack as a separate download, and because Java 5 annotations were not yet used, JAXB generated far more code during marshaling and unmarshaling. That code was tricky to read and more rigid to work with. For one thing, JAXB 1.0 generated Java interfaces for everything during unmarshaling, which bloated and complicated the code base, sometimes representing classes with counterintuitive names.

Java SE 1.6.0_05 comes with version 2.1.3 of JAXB. This includes the XJC tool in its bin directory, so as long as Java is on your path, which it ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required