Xstream, how to avoid a field with a specific value when marshaling

I want to create xml in the following format

<parm-list>
<param>
<NAME>somename</NAME>
<VALUE>somevalue</VALUE>
</param>
<param>
<NAME>somename</NAME>
<VALUE>somevalue</VALUE>
</param>
<param>
<NAME>somename</NAME>
<VALUE>somevalue</VALUE>
</param>
<param>
<NAME>somename</NAME>
<VALUE>somevalue</VALUE>
</param>
</param-list>

What can I do if I do not need a field <PARAM>with a specific one <NAME>in it?

+5
source share
1 answer

Although I really do not think that Xstream is really suitable for this task, it seems possible.

You can implement your own converter similar to one of these . If you look at the Converter interface, you will see that you can easily skip an element without writing anything to HierarchicalStreamWriter.

public class ParamConverter implements Converter{

    boolean canConvert(Class type){
        return Param.class.equals(type);
    }

    public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context){
        Param param = (Param)source;
        if (NAME_TO_SKIP.equals(param.getName()){
            return;
        }
        // delegate to ReflectionConverter or something else appropriate.
    }   
}

To register the converter just call xStram.registerConverter(new ParamConverter());.

, , .

+1

All Articles