Browse DevX
Sign up for e-mail newsletters from DevX


Generate XML Mapping Code with JAXB : Page 4

If you write any DOM or SAX code, you need to learn Java Architecture for XML Binding (JAXB). It rapidly generates XML mapping code for you, saving time and effort, and reducing both costs and risks.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Customization Through the Schema
Sometimes the code generated from the schema won't meet your exact requirements. Sometimes the schema has defects in it. Sometimes you just want to play around with things. In all of these cases, you can affect the behavior of the code generator through customization. At the moment, the reference implementation (RI) implements customization directly through the schema being processed, but the specification says that external customization scripts eventually will be supported too.

Customizing the schema won't affect the schema itself, except when it's being processed by xjc because it's done through a set of schema extension points. The spec allows for many customization points, at many levels of generation. This article doesn't have room for a detailed examination of these or a detailed explanation of schema extensions, but for a sample I'll show how to generate Vectors rather than Lists.

A schema extension is basically detail you add to the schema to give instructions and extra information to specific processors. In this case, the extensions are aimed at xjc and all other processors should ignore them.

I'm adding a global customization, which means that it affects all generated code. While this is a simple customization to make, quite a few other more complex ones provide real power to JAXB.

Here are the changes you must make to the schema to make JAXB generate java.util.Vector rather than List, its default collection (note that the top-level schema tag needs to be changed too):

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jxb:version="1.0"> <xs:annotation> <xs:appinfo> <jxb:globalBindings collectionType="java.util.Vector" </xs:appinfo> </xs:annotation> ...

Without going into too much detail about schemas, the annotation tag introduces a part of the schema that is usually intended for schema processing software. The appinfo tag introduces instructions for a particular processing application (in this case, JAXB's xjc code-generation tool). Usually, each application uses its own namespace, as JAXB has done here.

As it happens, simply changing the default collection type from List to Vector has no effect at all on the sample application I've presented here. My code works by retrieving iterators from collections, not the collections themselves. So when I generate the new model from this change, my code works without any new faults.

Some people are leery of altering their schemas. The standard supports external annotations, but the current implementation doesn't. This is something that will be rectified in the future.

You're on Your Way
That's all you need to know for a basic introduction to JAXB. Currently, the RI is in beta, but a final version is expected to be released around mid-February—so you don't have long to wait. The full release isn't going to differ much from the beta (apart from bug fixes), so you can start developing immediately against the beta fairly safely and then change over to the official first version when it's released.

If you decide to use JAXB technology, read the specification document from Sun, and pay particular attention to the section on customization. The customization I've introduced here really is trivial. JAXB is capable of considerably more.

Bryan Dollery is an experienced Java trainer and software engineer living in New Zealand. He recently presented papers at three major international software development conferences (in NZ and Australia). Reach him by e-mail at Bryan.Dollery@ChaosEngineers.co.nz.
Thanks for your registration, follow us on our social networks to keep up-to-date