Search code examples
javaxstream

XStream empty tag when List is empty


When using XStream, I get am empty tag(on the collection field) if I try to serialise an Object has java.util.List collection which is empty. How to I remove that empty tag in the xml output?


Solution

  • Your List variable must be null and not containing an empty list to avoid the 'empty' tag.

    List list = getList();
    if(list.isEmpty()) { list = null; }
    
    // Serialization ...
    

    EDIT:

    If you want that to be done automatically you need to create a custom converter, follow this tutorial: http://x-stream.github.io/converter-tutorial.html