在 Jaxb 中编组地图时出现问题
我有一个包含人类地图的world类。如果我编组类世界,我会得到以下输出:
<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);
}
}
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>
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>
Is this possible?
Here is the code of class world and human:
@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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 ilcavero 所指出的,
XmlAdapter
可用于将替代映射应用于 Map (或任何类型)在 JAXB 中。下面是具体示例的链接:了解更多信息
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:For More Information
我的解决方案
我将关键属性添加到 human ,然后将地图转换为数组:
My solution
I added the key attribute to human and then I converted the map to an array:
我认为您正在寻找 XmlValue 注释: http://jaxb.java.net/tutorial/section_6_2_7_4-Mapping-a-Class-to-Simple-Content-or-Simple-Type-XmlValue.html#Mapping%20a%20Class%20to% 20Simple%20Content%20or%20Simple%20Type:%20XmlValue
在您的情况下,它将应用于HumanEntry.value 而不是 XmlElement
I think you are looking for the XmlValue annotation : http://jaxb.java.net/tutorial/section_6_2_7_4-Mapping-a-Class-to-Simple-Content-or-Simple-Type-XmlValue.html#Mapping%20a%20Class%20to%20Simple%20Content%20or%20Simple%20Type:%20XmlValue
In your case it would be applied to HumanEntry.value instead of XmlElement