Java 列表根元素的 XStream 别名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3824362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XStream Alias of List root elements
提问by efleming
I want to be able to alias the root list element depending upon what type of objects are contained in the list. For example, this is my current output:
我希望能够根据列表中包含的对象类型为根列表元素设置别名。例如,这是我当前的输出:
<list>
<coin>Gold</coin>
<coin>Silver</coin>
<coin>Bronze</coin>
</list>
And this is what I want it to look like:
这就是我想要的样子:
<coins>
<coin>Gold</coin>
<coin>Silver</coin>
<coin>Bronze</coin>
</coins>
I can do this at a global level by saying all lists should be aliased to coins, but I have a lot of different lists and this won't work. Any ideas on how to do this? Seems like it should be simple, but of course, it isn't.
我可以通过说所有列表都应该别名为硬币来在全球范围内做到这一点,但是我有很多不同的列表,这行不通。关于如何做到这一点的任何想法?看起来它应该很简单,但当然,它不是。
EDIT: I should specify, I am trying to serialize objects to xml. I am using Spring 3 MVC as my web framework.
编辑:我应该指定,我正在尝试将对象序列化为 xml。我使用 Spring 3 MVC 作为我的 Web 框架。
采纳答案by pablosaraiva
Let's say you have a Coin class with a type attribute, as follows:
假设您有一个带有 type 属性的 Coin 类,如下所示:
@XStreamAlias("coin")
public class Coin {
String type;
}
And you have a Coins class that constains a List of Coin:
你有一个包含硬币列表的 Coins 类:
@XStreamAlias("coins")
public class Coins{
@XStreamImplicit
List<Coin> coins = new ArrayList<Coin>();
}
Pay attention to the annotations. The List is Implicit and the Coins class will be shown as "coins".
注意注释。该列表是隐式的,Coins 类将显示为“coins”。
The output will be:
输出将是:
<coins>
<coin>
<type>Gold</type>
</coin>
<coin>
<type>Silver</type>
</coin>
<coin>
<type>Bronze</type>
</coin>
</coins>
It's not the same you asked for, but there is a reason.
这与您要求的不一样,但这是有原因的。
At first, coin have only one attribute, but we are not sure if all objects you want to show do have only one attribute too. So, we need to tell which object attribute we are talking about.
起初,硬币只有一个属性,但我们不确定您要展示的所有对象是否也只有一个属性。所以,我们需要知道我们谈论的是哪个对象属性。
You can also show the Coin attributes as XML Attributes, not fields. As follows:
您还可以将 Coin 属性显示为 XML 属性,而不是字段。如下:
@XStreamAlias("coin")
public class Coin {
@XStreamAsAttribute
String type;
Coin(String type) {
this.type = type;
}
}
Here is the output:
这是输出:
<coins>
<coin type="Gold"/>
<coin type="Silver"/>
<coin type="Bronze"/>
</coins>
Hope it helps.
希望能帮助到你。
回答by Greg Case
This isn't an ideal solution, as it requires a separate wrapper class, but you could do something like this:
这不是一个理想的解决方案,因为它需要一个单独的包装类,但您可以执行以下操作:
public class CoinResponse {
private List<Coin> coinList;
public CoinResponse(List<Coin> coinList) {
this.coinList = coinList;
}
public List<Coin> getCoins() {
return this.coinList;
}
}
And here's the ugly part:
这是丑陋的部分:
List<Coin> coins = Arrays.asList( new Coin(), new Coin(), new Coin());
CoinResponse response = new CoinResponse(coins);
XStream xstream = new XStream();
xstream.alias( "coins", CoinResponse.class );
xstream.addImplicitCollection( CoinResponse.class, "coinList" );
System.out.println(xstream.toXML(response));
Basically, this is telling Xstream to use "coins" when converting the CoinResponse, and then don't use any name at all for the list itself.
基本上,这告诉 Xstream 在转换 CoinResponse 时使用“硬币”,然后根本不为列表本身使用任何名称。
回答by Bujji
@XStreamAlias("coins")
public class Coins {
@XStreamImplicit(itemFieldName="coin")
List<String> coins = new ArrayList<String>();
}