We showed how to transform xml to java arraylist with xstream in this example we will show how to convert xml to a java hashmap and vice versa. In our set up method we created a Restaurant object where we will initialize a map with guava with the restaurant's id as the key and the object as the map's value.
Setup
Serialize arraylist to Map
Using the map we created in the set up method we will marshall it to xml. Initializing XStream object, a simple facade to the xstream library, we will call toXML passing in the arraylist which will convert the object to xml. The output of the xml String is listed below.
Output
Deserialize xml to java map
Using the xml string above we removed any line breaks from a string and replaced it with correct escape codes. Since xmlAsMap is quite lengthy we only showed a portion of the string. Then passing the string into fromXML we will unmarshall or deserialize the xml into a java hashmap.