tags:

views:

26

answers:

1

Hi,

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?

+2  A: 

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://xstream.codehaus.org/converter-tutorial.html

Alois Cochard
where should I set the list to null?
walters
you must set your list to null (if empty) before serializing. but please provide code if you want a more helpful answer.
Alois Cochard
Sure! That would work.But, it gets messy if I have a parent-child relationship and parent has a list of children, and the children also maintain a list of other children for example. Does it imply that I have to check every list defined to ensure it's null if it's empty? Isn't there an effective way, because this can work in a simple object graph, but once this increases, it can get to be ineffective.
walters
No you need to create a custom converter, it's way smarter. I edited answer with link to tutorial.
Alois Cochard