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:

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.

Can you post proper XML using help provided here http://en.support.wordpress.com/code/posting-source-code
.
Also give little meaningful names to your XML tags. What I’ve understood out of your question is you have several “ertert” tags, same way I had several “message” tags. Is it that, you don’t want to wrap ’em the way I wrapped under “messages”?

Hi Sushant.. thx for this post.. its truly helpful for me. I am having few doubts like… in my previous project I was parsing XML file, size was around 40 to 50 MB and it was containing around 1lakh rows to insert data into DB. Would this tool works fine with larger records like 100000 rows??. Thanks in advance…:)

Yogesh, based on your requirements, first you need to decide whether you need XML Parsing or Binding mechanism. JAXB is Sun specification for XML binding with reference implementation, that I’ve used in my post.
Following article will be helpful to get a better idea, for tool selection in your situation.http://www.ibm.com/developerworks/xml/library/x-databdopt2

As I see your Payment class is having a potential candidate “sequence” to be key of the MAP. I assume Payment class will have optional members like “billing_method” or “person_id”, if this is the case you can implement that easily.

Or if you want to have a MAP of key-value pair, populate your map in a class which is extending the XmlAdapter the way you want. Still you will have to have Payment class which will have all possible annotated members coresponding to elements in XML.

So we need to have a property (here messages) under root element (profile) at our class level.
Alternatively you may have an array of Message object as property of Profile and that can be populated without using XMLAdapter.

If you need further investigation, I may try to get some time out over the coming weekend.

I’m having the same issue as devcodeur. I’ve tried every example I could find – additionally to just reading the docs and trying to code it myself: I have a list of elements in my XML and would like to map them to a map(string-key, element-class) in my code. Using the XmlAdapter I cannot get rid of the surrounding element (like devcodeur), which is named like my annotation (name=x) – the elements themselves are named “item”. And I’m returning a primitive array typed as my domain class from the XmlAdapter. Do you have an idea what’s wrong here?