4

JAXB doesn't let you unmarshal already existing xml structures into HashMaps if they are not exactly the way JAXB expects them.

JAXB is fine with handling e.g. LinkedLists and filling them.

I was thinking of creating a interface with a getKey() method and a wrapper around the HashMap taking all objects that implement that interface. The wrapper can then use the getKey() method for all key related features of the map. The wrapper could then easily implement the Collection or List interface.

Because this idea doesn't seem to innovative to me I presume that it already exists in some package, but I'm not googling correctly for it... Can someone please name a good lib that can do this, or do I have to code this myself?

Franz Kafka
  • 10,623
  • 20
  • 93
  • 149
  • Possible duplicate of http://stackoverflow.com/questions/7793283/problems-marshalling-a-map-in-jaxb. Regardless, the answer may be relevant. – Ed Staub Oct 18 '11 at 22:55

2 Answers2

3

You might consider extending ForwardingList of guava, and using a HashMap in the back. I don't know of any implementation that will leave you only the actual mapping.
Another alternative is creating JAXB XmlAdapter to adapt the values to your map. I think this one is more appropriate.

oshai
  • 14,865
  • 26
  • 84
  • 140
0

If all you are trying to pass the information content of a Map as a Collection, use Map.entrySet(). That gives you a Set<Map.EntrySet<K,V>> object; i.e. a collection whose elements are the key/value pairs of the Map. To reconstruct a Map from the collection, you will need to iterate the set and perform an put for each element.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Thanks, no I want to add stuff to the map. But I don't want to save stuff in a list first to then copy it to a map, just because of JAXB – Franz Kafka Oct 18 '11 at 22:49