【发布时间】:2014-07-12 01:31:25
【问题描述】:
我有以下实体:
@XStreamAlias("entity")
public class MapTestEntity {
@XStreamAsAttribute
public Map<String, String> myMap = new HashMap<>();
@XStreamAsAttribute
public String myText;
}
我将它与 xstream 一起使用,例如:
MapTestEntity e = new MapTestEntity();
e.myText = "Foo";
e.myMap.put("firstname", "homer");
e.myMap.put("lastname", "simpson");
XStream xstream = new XStream(new PureJavaReflectionProvider());
xstream.processAnnotations(MapTestEntity.class);
System.out.println(xstream.toXML(e));
并得到以下xml:
<entity myText="Foo">
<myMap>
<entry>
<string>lastname</string>
<string>simpson</string>
</entry>
<entry>
<string>firstname</string>
<string>homer</string>
</entry>
</myMap>
</entity>
但我需要将HashMap 映射到 xml 中的属性,例如:
<entity myText="Foo" lastname="simpson" firstname="homer" />
如何使用 XStream 做到这一点?我可以使用自定义转换器或映射器或类似的东西吗?蒂亚!!
(当然我的代码需要确保xml属性中没有重复。)
【问题讨论】: