I have a cool world that contains a map of people. If I marshal the world of classes, I get the 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 do not want to display the "value" -Tag. It should look like this:
<world> <humans> <human key="2"> <name>Tom</name> </human> <human key="1"> <name>Max</name> </human> </humans> </world>
Is it possible?
Here is the code of the world of class and man:
@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); } }
source share