Java: Using XMLAdapter to process custom datatypes using JAXB

JAXB provides a framework for two-way binding between XML and Java structures, making it easy for developers to serialize and deserialize their XML into Java objects.

Decorating a class with @XmlElement and @XmlAttribute annotations is all it usually takes to build a rich domain model from XML, but sometimes you get into a situation where more custom processing is required.

A custom XMLJavaTypeAdapter can help resolve these issues by providing custom methods for marshalling and unmarshalling the XML into the correct Java type.

Here are several examples of XMLJavaTypeAdapter usage:

  • An attribute containing a password string needs to be encrypted while on disk, but decrypted when loaded into the Java domain model
  • A string attribute value which represents a TRUE/FALSE boolean is sloppily represented by many different string values in the XML: “y”, “n”, “0”, “1”, “yes”, “no”, “n/a”, “”
  • A Base64 string value in an element represents an image, but needs to be converted to a byte array in the Java domain model
  • An attribute contains a local filename, this file needs to be read from disk and used as the true attribute value
  • There are a limited number of choices for an attribute, and you want to represent this as a Java enumeration

Sample Project

In this article I’ll be presenting a sample Java project that deserializes and serializes an address book, similar to what might be used in an instant messenger application to talk to your friends and contacts.

Each address book entry has two fields that need custom serialization:

  • The gender attribute string which needs to transformed to a Java enumeration type
  • The profile picture for a user in PNG format, which is base64 encoded in the XML but needs to be represented in Java as a byte array

A simple JUnit test exercises the serialization, from XML to Java objects and back to XML.

XML Explanation

An <addressbook> is the root object, containing <entries>.  Then each <entry> represents a person.  Each user has a name, an optionally specified gender, and optionally a small png image as their profile picture in Base64 format.

 <entry name="Amy" gender="F"/>
 <entry name="Bob">
 <avatarIcon type="png">iVBORw0KGg...U5ErkJggg==</avatarIcon>
 <entry name="Candice" gender="F">
 <avatarIcon type="gif">iVBORw0KGg...ORK5CYII=</avatarIcon>
 <entry name="David" gender="n/a"/>
 <entry name="Ernest" gender="m"/>

We will have JAXB turn this into a Java object graph where AddressBook is the main class, which contains a list of AddressEntry.

Each AddressEntry has a String value representing the name, a Gender class that contains a Java enumeration, and an AvatarIcon that contains an image byte array of their profile picture.

Notice that the ‘gender’ field is an XML attribute, whereas <avatarIcon> is an element.  We will create custom adapters for both these types.

Run Test

Before we go into the mechanics of adapters, let’s first download the sample project from github, package it, then run the JUnit test.

$ sudo apt-get install git openjdk-7-jdk maven -y
$ git clone
$ cd xmladaptertest
$ mvn test

This should result with the following JUnit message toward the bottom:

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

With the XML being output in three different forms: text, XML, and then HTML.

 T E S T S
Running xmladaptertest.TestReadXML
Address book:
 [name=Bob,gender=UNSPECIFIED,avatar=580 bytes]
 [name=Candice,gender=FEMALE,avatar=695 bytes]

The HTML version written to the console.

<h1>Address Book</h1>
<table width="400" border="1">
<tr><td>Bob</td><td>UNSPECIFIED</td><td><img src="Bob.png"/></td></tr>
<tr><td>Candice</td><td>FEMALE</td><td><img src="Candice.png"/></td></tr>

The HTML is also written to a file named “addressbook.html” in the root project directory.  When opened with a browser it should look like the image below.






Custom Objects in object graph

Now let’s look at, which is where we want to apply custom binding logic to our Gender attribute and AvatarIcon element.

public class AddressEntry {
 protected String name;

 protected Gender gender; 

  protected AvatarIcon avatar;

The Gender class is annotated as an @XmlAttribute.  Then we annotate a the field level with @XmlJavaTypeAdapter and instruct JAXB that we want to use the GenerEnumAdapter.class for marshalling and unmarshalling this XML attribute.

GenderEnumAdapter as shown below extends XmlAdapter:

public class GenderEnumAdapter extends XmlAdapter<String,Gender>

And then has an overriden unmarshal() method, that tells JAXB how to take the String coming from the XML and turn it into a Gender object that has an enumeration representation.

    public Gender unmarshal( String value )
    	if(null==value) {
    		return new Gender(GenderEnum.UNSPECIFIED);
    	}else if(value.equalsIgnoreCase("m") || value.equalsIgnoreCase("male")) {
    		return new Gender(GenderEnum.MALE);
    	}else if(value.equalsIgnoreCase("f") || value.equalsIgnoreCase("female")) {
    		return new Gender(GenderEnum.FEMALE);
    	}else {
    		return new Gender(GenderEnum.CHOOSE_NOT_TO_SPECIFY);

Conversely, it also has an overriden marshal() method which tells JAXB how to take the Gender object and turn it back into a String for XML.

    public String marshal( Gender value )
    	if(null==value) {
    	}else {
    		return value.toString();

The AvatarIcon class is annotated as an @XmlElement.  Notice that we did not annotate it with @XmlJavaTypeAdapter as above.  Instead, we have taken an alternate method and created a special class called that specifies for a whole package that anytime a certain Java type is serialized, that a specific adapter should be used.

Inside, you’ll see this line:


The AvatarIconAdapter extends XmlAdapter:

public class AvatarIconAdapter extends XmlAdapter<String,AvatarIcon>

And then has an overriden unmarshal() method, that tells JAXB how to take the String coming from the XML and turn it into an AvatarIcon object that contains a byte array representation of the image.

    public AvatarIcon unmarshal( String value )
    	if(null==value) return null;
    	return new AvatarIcon(javax.xml.bind.DatatypeConverter.parseBase64Binary(value));

Conversely, it also has an overriden marshal() method which tells JAXB how to take the Avataricon object and turn it back into a base64 string for XML.

    public String marshal( AvatarIcon value )
    	if(null==value.getImage()) return null;
    	return javax.xml.bind.DatatypeConverter.printBase64Binary(value.getImage());


REFERENCES (JAXB Arch) (JAXB tech nodes Java8) (User Guide) (custom XMLAdapter) ( (XMLJavaTypeAdapter on class) (@XmlJavaTypeAdapter for Base64) (embed base64 into HTML img tag) (xjc task from pom, schema to java) (base64 icons)