Convert xml to hashmap using xstream

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

class Restaurant {

    private Integer id;
    private String name;
    private String address;

    //...
}

Map<Integer, Restaurant> resturantByKey;

@Before
public void setUp() {

    Restaurant resturant = new Restaurant(1, "Woodlawn super club",
            "Fort Atkinson");
    Restaurant resturant2 = new Restaurant(2, "Sammy's", "Fort Atkinson");
    Restaurant resturant3 = new Restaurant(3, "ColdSpring Inn", "Cold Spring");

    restaurantByKey = Maps.newHashMap();
    restaurantByKey.put(1, resturant);
    restaurantByKey.put(2, resturant2);
    restaurantByKey.put(3, resturant3);
}

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.

@Test
public void serialize_map_to_xml() {

    XStream xStream = new XStream();
    xStream.alias("map", java.util.Map.class);

    String xml = xStream.toXML(restaurantByKey);

    logger.info(xml);

    assertNotNull(xml);
}

Output

<map>
  <entry>
    <int>1</int>
    <com.levelup.java.xml.XMLToMapXstream_-Resturant>
      <id>1</id>
      <name>Woodlawn super club</name>
      <address>Fort Atkinson</address>
      <outer-class>
        <resturantByKey reference="../../../.."/>
      </outer-class>
    </com.levelup.java.xml.XMLToMapXstream_-Resturant>
  </entry>
  <entry>
    <int>2</int>
    <com.levelup.java.xml.XMLToMapXstream_-Resturant>
      <id>2</id>
      <name>Sammy&apos;s</name>
      <address>Fort Atkinson</address>
      <outer-class reference="../../../entry/com.levelup.java.xml.XMLToMapXstream_-Resturant/outer-class"/>
    </com.levelup.java.xml.XMLToMapXstream_-Resturant>
  </entry>
  <entry>
    <int>3</int>
    <com.levelup.java.xml.XMLToMapXstream_-Resturant>
      <id>3</id>
      <name>ColdSpring Inn</name>
      <address>Cold Spring</address>
      <outer-class reference="../../../entry/com.levelup.java.xml.XMLToMapXstream_-Resturant/outer-class"/>
    </com.levelup.java.xml.XMLToMapXstream_-Resturant>
  </entry>
</map>

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.

@Test
public void deserialize_xml_to_hashmap() {

    String xmlAsMap = "<map> <entry>";

    XStream xStream = new XStream();
    xStream.alias("map", java.util.Map.class);

    @SuppressWarnings("unchecked")
    Map<Integer, Restaurant> restaurantConverted = (Map<Integer, Restaurant>) xStream
            .fromXML(xmlAsMap);

    assertThat(resturantConverted, hasKey(new Integer("1")));

}