XMLAdapter for HashMap
Posted
by
denniss
on Stack Overflow
See other posts from Stack Overflow
or by denniss
Published on 2011-02-11T23:24:01Z
Indexed on
2011/02/11
23:25 UTC
Read the original article
Hit count: 219
I want to convert a list of items inside of my payaload and convert them into a hashmap. Basically, what I have is an Item xml representation which have a list of ItemID. Each ItemID has an idType in it. However, inside my Item class, i want these ItemIDs to be represented as a Map.
HashMap<ItemIDType, ItemID>
The incoming payload will represent this as a list
<Item>...
<ItemIDs>
<ItemID type="external" id="XYZ"/>
<ItemID type="internal" id="20011"/>
</ItemIDs>
</Item>
but I want an adapter that will convert this into a HashMap
"external" => "xyz"
"internal" => "20011"
I am right now using a LinkedList
public class MapHashMapListAdapter extends XmlAdapter<LinkedList<ItemID>, Map<ItemIDType, ItemID>> {
public LinkedList<ItemID> marshal(final Map<ItemIDType, ItemID> v) throws Exception { ... }
public Map<ItemIDType, ItemID> unmarshal(final LinkedList<ItemID> v) throws Exception { ... }
}
but for some reason when my payload gets converted, it fails to convert the list into a hashmap. The incoming LinkedList of the method unmarshal is an empty list. Do you guys have any idea what I am doing wrong here? Do I need to create my own data type here to handle the LinkedList?
© Stack Overflow or respective owner