JAXB stands for Java Architecture for XML-Binding. JAXB maps xml-types to java-classes and vice-versa.
The reference implementation of this java specification causes some problems when used with current Java 6. Java 6 ships with its own JAXB 2.0 implementation that is not compatible to the most current versions JAXB 2.1 and 2.2.
If you use this versions in your applications you have to put the jaxb-api.jar into the JAVA_HOME/lib/endorsed folder of your Java Runtime Environment (JRE). An alternative is to launch your java-application with the runtime-property “java.endorsed.dirs” set to the location of the jaxb-api.jar. For both alternatives you need to ship the implementation jars (e.g. jaxb-impl.jar, …) with your application.

I encountered an additional problem when switching from JAXB 2.1 to 2.2. There was a fix in JAXB 2.1 that improved parsing of abstract xml-types. This enables JAXB to parse xsi:type attribute which allows to use abstract XML-Types that are mapped on an abstract Java-Classes. The concrete type will be declared using xsi:type attribute. This fix was disabled in JAXB 2.2 for some reason. And it have to be enabled using the JAXBContext property com.sun.xml.bind.improvedXsiTypeHandling . The corresponding Bug can be found here: http://java.net/jira/browse/JAXB-620.

You have to set JAXBContext property com.sun.xml.bind.improvedXsiTypeHandling to true.

How to set properties
If you want to set properties on startup of JVM you need to use the switch -D (e.g. java -Djava.endorsed.dirs={path_to_lib} …)

Example of how to use abstract xml-types mapped on abstract classes in java using JAXB

<xsi:complexType name="Animal" abstract="true">
  <xsi:sequence>
    <xsi:element name="name" type="xsi:string"/>
  </xsi:sequence>
</xsi:complexType>
<xsi:complexType name="Fish">
  <complexContent>
     <xsi:extension base="a:animal">
       <sequence>
         <element name="amountOfFins" type="xsi:integer" />
       </sequence>
     </xsi:extension>
  </xsi:complexContent>
</xsi:complexType>
<xsi:complexType name="Mammal">
  <complexContent>
     <xsi:extension base="a:animal">
       <sequence>
         <element name="amountOfLegs" type="xsi:integer" />
       </sequence>
     </xsi:extension>
  </xsi:complexContent>
</xsi:complexType>
<xsi:element name="animalInstance" type="a:animal"/>

An instance of an animal could look like this:

<?xml version="1.0"?>
<animalInstance xsi:type="a:Mammal" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:a="http://....../Animal">
  <a:name>Dog</a:name>
  <a:amountOfLegs>4</a:amountOfLegs>
</anInstance>

JAXB is able to unmarshal this instance correctly and create a mammal Java-object. Marshalling is also no problem. So you are able to write generic methods for animals. This is useful especially for WebServices (e.g. using JAXWS). So you don’t have to implement a WebService-operation for every subtype. One operation handling the abstract animal-type is sufficient.

More information on JAXB 2.0

http://jaxb.java.net/guide/Migrating_JAXB_2_0_applications_to_JavaSE_6.html

Sorry, the comment form is closed at this time.

 
© 2011 Softwareengineering in practise Suffusion theme by Sayontan Sinha