【发布时间】:2015-01-28 11:22:12
【问题描述】:
我想将对象转换为需要为字符串数据类型提供别名的 xml。例如:
public class ArrayTest
{
private int id=4;
public String area[];
public void setArea(String ar[])
{
area = ar;
}
}
对象到xml的转换类是这样的:
public class Test
{
public static void main(String args[])
{
String area[] = {"ABC","XYZ","PRQ"};
ArrayTest at = new ArrayTest();
at.setArea(area);
Xstream stream = new XStream(new staxDriver());
stream.alias("response",ArrayTest.class);
System.out.println(stream.toXML(at));
}
}
我得到的输出是:
<?xml version="1.0" ?>
<response>
<id>4</id>
<area>
<string>ABC</string>
<string>XYZ</string>
<string>PRQ</string>
</area>
</response>
但我想输出如下:
<?xml version="1.0" ?>
<response>
<id>4</id>
<area>
<code>ABC</code>
<code>XYZ</code>
<code>PRQ</code>
</area>
</response>
我是 XStream 新手,请帮帮我
【问题讨论】:
-
我猜
@XStreamImplicit(itemFieldName="code")超过public String area[];是你要找的东西:xstream.codehaus.org/… -
@Albert:我也试过了,但输出不是我所期望的......
-
如@jjlema 所说,以编程方式尝试。
-
@Albert Itried 这种方式也...请参阅下面的评论我提到的输出不是我所期望的......如果可能的话,请提供一些其他方法