本文介绍了在Jaxb编组地图的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含人类地图的世界类。如果我编组课堂世界,我得到以下输出:
I have a class world that contains a map of humans. If I marshal the class world I get following output:
<world>
<humans>
<human key="2">
<value>
<name>Tom</name>
</value>
</human>
<human key="1">
<value>
<name>Max</name>
</value>
</human>
</humans>
</world>
但是我不想显示值-Tag。它应该看起来像:
But I dont want to display the "value"-Tag. It should look like:
<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);
}
}
推荐答案
被指出, XmlAdapter
可用于应用替代映射到JAXB中的Map(或任何类型)。以下是一个具体示例的链接:
As was pointed out by ilcavero an XmlAdapter
can be used to apply an alternative mapping to Map (or any type) in JAXB. Below is a link to a concrete example:
- http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html
更多信息
- http://blog.bdoughan.com/search/label/XmlAdapter
这篇关于在Jaxb编组地图的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!