XStream – Collection Converters

In the earlier tutorials we saw how to convert a Java object to XML and back, custom converter and basic converters. In this tutorial we look at how XStream converts array and collections from java to xml and vice versa. We will convert the following types :

  1. String[]
  2. char[]
  3. List&ltString&gt
  4. java.util.Properties
  5. List&ltString&gt
  6. Map&ltString, String&gt
  7. TreeMap&ltString, String&gt treeMap
  8. TreeSet&ltString&gt treeSet
  9. enum
  10. EnumMap&lttestEnum, String&gt testEnum is an enum
  11. EnumSet&lttestEnum&gt testEnum is an enum

Here’s the complete example

package com.studytrails.xml.xstream;

import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
import java.util.TreeSet;

import com.thoughtworks.xstream.XStream;

public class ConverterExample2 {
	public static void main(String[] args) {
		ConverterExample2 example2 = new ConverterExample2();
		example2.runExample();
	}

	public void runExample() {
		XStream xStream = new XStream();
		CollectionConverterExample classContainingCollection = new CollectionConverterExample();
		classContainingCollection.initialize();
		String xml = xStream.toXML(classContainingCollection);
		System.out.println(xml);
	}
}

class CollectionConverterExample {

	String[] stringArray = new String[] { "StringA", "StringB", "StringC" };
	char[] charArray = new char[] { 'a', 'b', 'c', 'd', 'e' };
	List<String> listA = new ArrayList<String>();
	Map<String, String> mapA = new HashMap<String, String>();
	Properties properties = new Properties();
	List<String> singletonList;
	Map<String, String> singletonMap;

	TreeMap<String, String> treeMap = new TreeMap<String, String>();
	TreeSet<String> treeSet = new TreeSet<String>();
	enum testEnum {testA, testB}
	testEnum testeEnumValue = testEnum.testA;
	EnumMap<testEnum, String> testEnumMap = new EnumMap<CollectionConverterExample.testEnum, String>(testEnum.class);
	EnumSet<testEnum> testEnumSet = EnumSet.range(testEnum.testA, testEnum.testB);

	public void initialize() {
		listA.add("testA");
		mapA.put("keyA", "ValueA");
		properties.put("propertyA", "valueA");
		treeMap.put("treeA", "valueA");
		treeMap.put("treeB", "valueB");
		treeSet.add("treeB");
		treeSet.add("treeA");
		singletonList = Collections.singletonList("singletonListA");
		singletonMap = Collections.singletonMap("key1", "value1");
		
		testEnumMap.put(testEnum.testA, "testEnumMapValue1");
		testEnumMap.put(testEnum.testB, "testEnumMapValue2");

	}
	

}

The resultant XML looks like this


	
		StringA
		StringB
		StringC
	
	abcde
	
		testA
	
	
	
	  keyA
	  ValueA
	
	
	
		
	
	
		singletonListA
	
	
	
	  key1
	  value1
	
	
	
	
	  treeA
	  valueA
	
	
	  treeB
	  valueB
	
	
	
		treeA
		treeB
	

Leave a Reply

Your email address will not be published. Required fields are marked *