Skip to main content

Technology Preview of JAXB 2.0

Posted by kohsuke on February 1, 2005 at 7:32 AM PST

As Eduardo mentioned, we just released a preview of what JAXB 2.0 and JAX-RPC 2.0 look like. This bundle includes the current snapshot of the JAXB RI and JAX-RPC RI.
It is a "preview", so the documentation is very sketchy, and it's definitely not feature-complete, but it has enough things in it to give you more concrete idea about how JAXB and JAX-RPC will look like in 2.0.

Since my day job is to work on the JAXB RI, let me introduce some of the interesting things about the JAXB RI in this bundle.

JAXB 2.0 is not just a schema compiler

JAXB 1.0 was basically just a schema compiler. You compile a schema, you get Java classes, and that was it. But JAXB 2.0 is different. It is primarily a framework that allows arbitrary user-written classes to be mapped to arbitrary XML. This is done by defining a set of annotations.

JAXB 2.0 then defines a schema compiler on top of it, as a code generator that generates the annotated classes. But as far as the runtime is concerned, hand-written classes and compiler-generated classes are indistinguishable.

This adds a lot of flexibility. For example, you can compile a schema to quickly get the initial skeleton, then you can take the ownership of the classes and add your methods, have them implement some interfaces, or change access modifiers. And after all those changes, it will still work just the same!

When your schema is relatively stable, I found this to be a lot easier than learning JAXB customizations to get the code you want.

JAXB 2.0 for XML serialization

JAXB 2.0 can be used to serialize your objects to XML. This is handy when you want to save your data in a portable format. In a sense, it can be used like XStream, except that JAXB gives you more control over how your XML looks like.

In particular, we paid a lot of attention to make sure that JAXB 2.0 can read broken XML documents. Even if required elements/attributes are missing, elements are out-of-order, or unknown elements are added, it will still unmarshal to your classes.

For example, when you are using JAXB to communicate between clients and servers, or when you are using JAXB to save your application settings, this robustness is very handy, because you don't have to worry too much about compatibility between client/server or different versions of your application.

A bit about RI

I've talked a bit about the spec, but since I work on the RI, so let me brag a bit about the RI :-)

First, we put the runtime on diet. In JAXB RI 2.0, we compressed the runtime down to just about 500KB. This implementation jar, the JAXB API jar, and the StAX API jar are the only three files you will need to redistribute, and that's still below 600KB.

Second, we also reduced the size of the generated code drastically. There's no unmarshalling/marshalling/validation code in the generate code, and there's no imp/runtime package. It's just data fields, getters, and setters. I want it so simple and clean that you feel like start adding your code in it. There's still some distance to go, but there's already noticable improvements.

The schema compiler runs a lot faster now, with a lot less memory. We've been compiling the schema that IRS uses for e-filing, and that 6MB monster schema compiles just fine without even bumping up -Xmx.

Oh, and the RI also supports the compilation of DTD in this bundle, although the RELAX NG support is still on its way.

To try it out

So, it's a full of good stuff, so please try it out, and let us know how you think. See this document for the installation instruction and how to run it.

Related Topics >>