java - XStream Alias of List root elements -


i want able alias root list element depending upon type of objects contained in list. example, current output:

<list> <coin>gold</coin> <coin>silver</coin> <coin>bronze</coin> </list> 

and want like:

<coins> <coin>gold</coin> <coin>silver</coin> <coin>bronze</coin> </coins> 

i can @ global level saying lists should aliased coins, have lot of different lists , won't work. ideas on how this? seems should simple, of course, isn't.

edit: should specify, trying serialize objects xml. using spring 3 mvc web framework.

let's have coin class type attribute, follows:

@xstreamalias("coin") public class coin {     string type; } 

and have coins class constains list of coin:

@xstreamalias("coins") public class coins{      @xstreamimplicit     list<coin> coins = new arraylist<coin>(); } 

pay attention annotations. list implicit , coins class shown "coins".

the output be:

<coins>   <coin>     <type>gold</type>   </coin>   <coin>     <type>silver</type>   </coin>   <coin>     <type>bronze</type>   </coin> </coins> 

it's not same asked for, there reason.

at first, coin have 1 attribute, not sure if objects want show have 1 attribute too. so, need tell object attribute talking about.

you can show coin attributes xml attributes, not fields. follows:

@xstreamalias("coin") public class coin {     @xstreamasattribute     string type;      coin(string type) {         this.type = type;     } } 

here output:

<coins>   <coin type="gold"/>   <coin type="silver"/>   <coin type="bronze"/> </coins> 

hope helps.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -