映射一個HashMap來在XML鍵 - 值屬性我有以下實體:如何使用XStream的
@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?我可以使用自定義轉換器或映射器或類似的東西嗎? TIA!
(當然我的代碼必須確保在XML屬性沒有重複。)
它只能與Map一起使用,是嗎? –
Emi
@Emi你是對的。對我來說這就足夠了。 (但它可能會擴展爲使用可用的字符串表示法處理其他對象。) – t777