假设您有一个带有type属性的Coin类,如下所示:
@XStreamAlias("coin")public class Coin { String type;}并且您有一个Coins类,其中包含Coin列表:
@XStreamAlias("coins")public class Coins{ @XStreamImplicit List<Coin> coins = new ArrayList<Coin>();}注意注释。该列表是隐式的,Coins类将显示为“ coins”。
输出将是:
<coins> <coin> <type>Gold</type> </coin> <coin> <type>Silver</type> </coin> <coin> <type>Bronze</type> </coin></coins>
您要求的不一样,但这是有原因的。
首先,钱币只有一个属性,但是我们不确定要显示的所有对象是否也只有一个属性。因此,我们需要知道我们在谈论哪个对象属性。
您还可以将“硬币”属性显示为XML属性,而不是字段。如下:
@XStreamAlias("coin")public class Coin { @XStreamAsAttribute String type; Coin(String type) { this.type = type; }}这是输出:
<coins> <coin type="Gold"/> <coin type="Silver"/> <coin type="Bronze"/></coins>
希望能帮助到你。



