JAXB 可以将 ArrayList 输出为逗号分隔值吗?
我有类似的东西
@XmlElementWrapper(name="Mylist")
List<Items> myItems = new ArrayList<Items>()
,结果是这样的,
<Mylist>
<myItems>item 1</myItems>
<myItems>item 2</myItems>
<myItems>item 3</myItems>
</Mylist>
是否有可能使它看起来更像是
<Mylist>
<myItems>item 1, item 2, item 3</myItems>
</Mylist>
因为我所追求的数据无论如何都只是文本?
I have something like
@XmlElementWrapper(name="Mylist")
List<Items> myItems = new ArrayList<Items>()
and that comes out like
<Mylist>
<myItems>item 1</myItems>
<myItems>item 2</myItems>
<myItems>item 3</myItems>
</Mylist>
Is it possible to make this come out more like
<Mylist>
<myItems>item 1, item 2, item 3</myItems>
</Mylist>
Since the data I am after is all just textual anyway?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 @XmlList使其成为空格分隔的值。
对于逗号分隔的列表,您需要使用 XmlAdapter。有关 XmlAdapter 请参阅:
You can use @XmlList to make it a space separated value.
For a comma separated list you will need to use an XmlAdapter. For more information on XmlAdapter see:
这是一个用于处理逗号分隔列表的
XmlAdapter
:您可以像这样使用它:
Here's an
XmlAdapter
to handle comma separated lists:You would use it like this:
您可以通过使字段为瞬态
@XmlTransient
并创建一个用于计算以逗号分隔的字符串的方法来实现此目的。关于包装
如果确实有必要,您必须将列表包装到另一个对象中。you can accomplish that by making the field transient
@XmlTransient
and creating a method for calculating a String with a comma-separated.About the wrapping
<Mylist>
if it is really necessary you will have to wrap the list into a another object.