Will Goring Will Goring - 1 year ago 57
Java Question

customising serialisation of java collections using xstream

I have an object that needs to be serialised as XML, which contains the following field:

List<String> tags = new List<String>();

XStream serialises it just fine (after some aliases) like this:


That's OK as far as it goes, but I'd like to be able to rename the
elements to, say,
. I can't see an obvious way to do that from the alias documentation on the XStream site. Am I missing something obvious?

Answer Source

I'd suggest changing the List<String> to a List<Tag>, where Tag is a domain object that essentially just contains a String. Then you say:

xstream.alias("tag", org.goring.Tag.class);

and you get exactly what you want. This avoids having to roll your own Converter.