Java Reference
In-Depth Information
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/libdirectory webservices-tools.jar. You may also need webservices-rt.jaron 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
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 bindir-
ectory, so as long as Java is on your path, which it should be, you'll have no problem invoking
it.
Search WWH ::




Custom Search