Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help With JaxB Unmashalling

 
Darren Carter
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

I have a XML docment in which I need to unmarshall into a java object using jaxb. I am a little unclear of how I should map the xml annotations in my pojo and was hoping someone could advise?

my XML document is



Pojo



Can any one tell me how I would map this using annotations? so for example card_name should be mapped to cardName.

Thanks
D.

 
Karthik Shiraly
Bartender
Posts: 1208
25
Android C++ Java Linux PHP Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the <struct> and <member> tags, this looks to me like the response of an XML-RPC web service.

In my opinion, this kind of response is not very convenient to process using JAXB, though I think it is possible using custom unmarshalling adaptor. If getting a Card object by other simpler methods is ok, then read on.

You can use Apache XMLRPC library (Download link), since it can interpret XMLRPC responses correctly.
It'll convert the <struct> element to a Map and each <member> to a Map entry.



Next, iterate through this Map's entries and convert the underscored keys to java camelcase (like "card_name" to "cardName") using Guava library's CaseFormat class

Finally, convert this Map to Card object using Apache BeanUtils. It'll also need Apache Collections 3.2.x framework to work.
 
Tony Evans
Ranch Hand
Posts: 598
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Would you not be better of generating the object from the XML schema that will sort out all your annotations for you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic