This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes Web Services and the fly likes JAXB mapping of HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "JAXB mapping of HashMap" Watch "JAXB mapping of HashMap" New topic

JAXB mapping of HashMap

Bharath Chinnadurai Maharajan

Joined: Dec 22, 2004
Posts: 26
Hi All,
XSD types does not include a HashMap as it includes a List. Is there a workaround in which i can use a HashMap the same way is use a List.

I have a need to send a HasMap of some data over the wire. Is there a workaround by which i can achieve this.

Peer Reynders

Joined: Aug 19, 2005
Posts: 2930
XML (Schema) has no idea what a HashMap is. However from a data perspective it is nothing but a list of pairs where each pair has a unique key and an associated value (in Java the pair is java.util.Map.Entry). So that data can easily be structured like:

This internal structure is exposed as a Set<Map.Entry<K,V>> through java.util.HashMap.entrySet(). java.util.Set.toArray() can then turn it into a array of Map.Entry<K,V> for which it shouldn't be too difficult to set up a mapping.

Of course you could simply put the Hashmap inside of a container class as JAXB can handle a HashMap as a return value from a bean - it just can't deal with it as a top level object. Mapping your favorite class (HashMap)

XML layout and in-memory data layout (XmlJavaTypeAdapter)
[ December 10, 2008: Message edited by: Peer Reynders ]
Don't get me started about those stupid light bulbs.
subject: JAXB mapping of HashMap
It's not a secret anymore!