JAXB, Custom XML to Java Map using XMLAdapter

In some cases XML structure that is specific to a Java container is very different, e.g. Map. And we want to bind our Java classes with a customized and more readable XML, JAXB XmlJavaTypeAdapter annotation comes very handy in such situations. Such mappings require an adapter class, written as an extension of XmlAdapter, and XmlJavaTypeAdapter annotation suggests JAXB to use the adapter at specified location:

For illustration purpose, we’ll have a custom XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
		<message id="1">
			<body>wat's up mike.. r u gonna catch us tonight?</body>
		<message id="2">
			<body>My apologies, forgot to tell ya, I'm out of town!!!</body>

And this is our JAXB annotated message class(minimal):

public class Message {
	private String id;
	private String subject;
	private String body;

And the profile class with required annotations, note that we have a HashMap of Message where message id is the key and Message object itself as a value. Also note the use of @XmlJavaTypeAdapter which provides mapping between custom XML and HashMap during the process of marshaling/ unmarshaling. Going forward we’ll see how we are going to achieve that.

public class Profile {
    private HashMap<String, Message> messages;
    public Profile(){}
    public Profile(HashMap<String, Message> b ){
    	messages = b;

To fill the gap we need to provide something that JAXB knows how to handle, we can use a wrapper class which contains Array of objects of type Messagse.

public class Messages {
    public Message[] messages;

Now you must have got a fair idea that how JAXB is going to read XML messages to Message array back and forth. Let’s write an adapter to map our Array to a HasMap.

public class MessageAdapter extends XmlAdapter<Messages,Map<String, Message>> {
    public Map<String, Message> unmarshal( Messages value ){
    	Map<String, Message> map = new HashMap<String, Message>();
        for( Message msg : value.messages )
            map.put( msg.getId(), msg );
        return map;

    public Messages marshal( Map<String, Message> map ){
        Messages msgCont = new Messages();
        Collection<Message> msgs = map.values();
        msgCont.messages = msgs.toArray(new Message[msgs.size()]);
        return msgCont;

And here is the test class to assert our assumptions:

public class XmlAdapterTest extends TestCase{
	public void testAdapter() throws Exception {
		InputStream is = this.getClass().getClassLoader().getResourceAsStream("profile.xml");
		if (is != null) {
			JAXBContext jc;
			try {
				//test unmarshaling
				jc = JAXBContext.newInstance(Profile.class.getPackage().getName());
				Unmarshaller u = jc.createUnmarshaller();
				Profile profile = (Profile) u.unmarshal(is);
				assertEquals( 2, profile.getMessages().size());
				//test marshaling
				Marshaller marshaller=jc.createMarshaller();
				File xmlDocument = new File("output.xml");
				marshaller.marshal(profile, new FileOutputStream(xmlDocument));
				assertTrue(xmlDocument.length() > 0);
			} catch (JAXBException e) {

Still need the source code, find here: jaxb-typeadapter source, Sushant