Xstream – 删除ArrayList元素名称

huangapple 未分类评论47阅读模式
英文:

Xstream - Remove ArrayList elements name

问题

我有一个类似的类:

class Account{
  List<String> information;
}

XStream序列化成XML如下所示:

<information>
      <string>This is sample Info 1</string>
 </information>

我希望XML看起来像这样:

<Information>
  This is sample Info 1
</Information>

所以我尝试使用:

xStream.aliasAttribute(Account.class,"information","Information");

现在我的输出XML如下所示:

<Information>
   <string>This is sample Info 1</string>
</Information>

如何摆脱XML中的<string>

英文:

I have a class like

class Account{
  List&lt;String&gt; information;
}

XStream serializes to XML that looks like:

&lt;information&gt;
      &lt;string&gt;This is sample Info 1&lt;/string&gt;
 &lt;/information&gt;

I wanted the XML like:

&lt;Information&gt;
  This is sample Info 1
&lt;/Information&gt;

so I tried using

xStream.aliasAttribute(Account.class,&quot;information&quot;,&quot;Information&quot;);

now I have output XML as:

&lt;Information&gt;
   &lt;string&gt;This is sample Info 1&lt;/string&gt;
&lt;/Information&gt;

How can I get rid of this &lt;string&gt; in XML?

答案1

得分: 0

我知道这已经相当旧了,但也许有人会发现这个答案很有用。要做到这一点,只需在@XStreamImplicit注解中使用itemFieldName属性。

所以在这种情况下:

@XStreamImplicit(itemFieldName="Information")
List<String> information = Arrays.asList("Sample 1", "Sample 2");

将会产生以下的XML结果:

<Information>Sample 1</Information>
<Information>Sample 2</Information>
英文:

I know this is quite old but perhaps someone will find the answer useful. To do that simply use @XStreamImplicit annotation with itemFieldName property.
So in this case:

@XStreamImplicit(itemFieldName=&quot;Information&quot;)
List&lt;String&gt; information = Arrays.asList(&quot;Sample 1&quot;, &quot;Sample 2&quot;);

Will result in following XML result:

&lt;Information&gt;Sample 1&lt;/Information&gt;
&lt;Information&gt;Sample 2&lt;/Information&gt;

huangapple
  • 本文由 发表于 2020年7月24日 00:01:29
  • 转载请务必保留本文链接:https://java.coder-hub.com/63058451.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定