我有一個類世界包含人類地圖。如果我編組世界我得到以下輸出:在Jaxb中編組地圖的問題
<world>
<humans>
<human key="2">
<value>
<name>Tom</name>
</value>
</human>
<human key="1">
<value>
<name>Max</name>
</value>
</human>
</humans>
</world>
但我不想顯示「價值」 - 標籤。它應該看起來像:
<world>
<humans>
<human key="2">
<name>Tom</name>
</human>
<human key="1">
<name>Max</name>
</human>
</humans>
</world>
這可能嗎?
這裏是一流的世界和人類的代碼:
@XmlRootElement
public class Human {
@XmlElement
private String name;
public Human(){}
}
@XmlRootElement
public class World {
private Map<String, Human> humans = new HashMap<String, Human>();
public World(){}
@XmlElementWrapper(name = "humans")
@XmlElement(name = "human")
public HumanEntry[] getMap() {
List<HumanEntry> list = new ArrayList<HumanEntry>();
for (Entry<String, Human> entry : humans.entrySet()) {
HumanEntry mapEntry =new HumanEntry();
mapEntry.key = entry.getKey();
mapEntry.value = entry.getValue();
list.add(mapEntry);
}
return list.toArray(new HumanEntry[list.size()]);
}
public void setMap(HumanEntry[] arr) {
for(HumanEntry entry : arr) {
this.humans.put(entry.key, entry.value);
}
}
public static class HumanEntry {
@XmlAttribute
public String key;
@XmlElement
public Human value;
}
public void addHuman(String key, Human human){
this.humans.put(key, human);
}
}
謝謝。查看我的答案獲得完整的解決方案。 –