Child pages
  • JAXB User Guide
Skip to end of metadata
Go to start of metadata

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:

See Purchase Order Sample for an working example.

Here's what mappings look like:

classpath:org/jvnet/annox/samples/po/package-info.ann.xml
classpath:org/jvnet/annox/samples/po/Items$Item.ann.xml
  • No labels

2 Comments

  1. 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?

    1. 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.