Will Goring Will Goring - 5 months ago 16
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:

<tags>
<string>tagOne</string>
<string>tagTwo</string>
<string>tagThree</string>
<string>tagFour</string>
</tags>


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

Answer

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.

Comments