JAXB User Guide

Overview

JAXB (Java Architecture for XML Binding) allows mapping Java classes to XML representations.

Starting from version 2.0, JAXB is heavily based on Java annotations to define Java/XML mappings. In order to be able to marshal your objects into XML and umarshall objects from XML, you first need to have your classes annotated with JAXB annotations. You can do this manually with your existing classes or you can use a schema compiler to compile your XML schema into a set of schema-derived classes which will contain all the required annotations.

The problem is that JAXB does not provide any alternatives to reading Java/XML mapping from annotations. If you're using JAXB, you're forced to have your mappings defined with Java annotations - with all the limitations I've mentioned in the user guide. Like, you can't annotate third-party classes, you can't provide alternative annotations for already annotated classes and so on.

Annox provides a solution for this problem.

JAXB reference implementation can be configured with a special annotation reader which may implement a different strategy for reading annotations. Annox takes advantage of this feature and implements an annotation reader which can load JAXB annotations from XML.

Using Annox annotation reader with JAXB RI

Before you start with Annox annotation reader for JAXB RI, I highly recommend reading the Annox user guide in order to understand core Annox concepts.

Using Annox annotation reader with JAXB RI is really very simple. You just have to create an instance of AnnoxAnnotationReader and set it as JAXBRIContext.ANNOTATION_READER property of the JAXB context:

final AnnotationReader<Type, Class, Field, Method> annotationReader = new AnnoxAnnotationReader();

final Map<String, Object> properties = new HashMap<String, Object>();

properties.put(JAXBRIContext.ANNOTATION_READER, annotationReader);

final JAXBContext context = JAXBContext.newInstance(
	"org.jvnet.annox.samples.po",
	Thread.currentThread().getContextClassLoader(),
	properties);

final Object myObject = context.createUnmarshaller().unmarshal( ... );

See Purchase Order Sample for an working example.

Comments

bryan jacobs says:

I'm interested in using this tool for JAXB.

The schema doesn't seem to support the @XmlRootElement(name  = "SomElement") annotation.

I'm not totally familar with JABX, but is that going to be a problem?

Aleksei Valikov says:

Which schema does not seem to support this?

Anyway, with Annox this will be something like

<class xmlns="http://annox.dev.java.net" xmlns:annox="http://annox.dev.java.net" xmlns:jaxb="http://annox.dev.java.net/javax.xml.bind.annotation">
<jaxb:XmlRootElement name="SomeElement"/>
...
</class>

See the purchase order sample to get started.


Browse Space

- Pages
- News
- Labels
- Attachments
- Bookmarks
- Mail
- Activity
- Advanced

Explore Confluence

- Popular Labels
- Notation Guide
- Impressum

Your Account

Log In

 

Other Features

Add Content