Oops Null Pointer

Java programming related

Monthly Archives: June 2011

XStream Removing no-comparator from XML output

When XStream exports a TreeSet to XML the output contains “no-comparator” and “comparator” nodes. For my clients the comparator node is not used and makes the XML less clear.

To remove these nodes from the output I added a customer converter:

public class IgnoreComparatorTreeSetConverter extends CollectionConverter {
    public IgnoreComparatorTreeSetConverter(Mapper mapper) {
    public boolean canConvert(@SuppressWarnings("rawtypes") Class type) {
        return type.equals(TreeSet.class);

    public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
        TreeSet<Object> result = new TreeSet<Object>();
        super.populateCollection(reader, context, result);
        return result;

And to use it just register the converter:

XStream xstream = new XStream(new DomDriver());
xstream.registerConverter(new IgnoreComparatorTreeSetConverter(xstream.getMapper()));