在 Jaxb 中编组地图时出现问题

发布于 2024-12-10 11:31:35 字数 1954 浏览 0 评论 0原文

我有一个包含人类地图的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧竹 2024-12-17 11:31:35

正如 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

蓝眼睛不忧郁 2024-12-17 11:31:35

我的解决方案

我将关键属性添加到 human ,然后将地图转换为数组:

@XmlRootElement
public class World {

    @XmlJavaTypeAdapter(HumanAdapter.class)
    private Map<String, Human> humans = new HashMap<String, Human>();

    public World(){}
}

public class Human {

    @XmlElement
    private String name;

    @XmlAttribute(name="key")
    private String key;

    public Human(){}
}

public class HumanAdapter extends XmlAdapter<HumanJaxbCrutch, Map<String, Human>> {

    @Override
    public HumanJaxbCrutch marshal(Map<String, Human> humans) throws Exception {
        List<Human> list = new ArrayList<Human>();
        for (Entry<String, Human> entry : humans.entrySet()) {
            list.add(entry.getValue());
        }

        HumanJaxbCrutch humanJaxbCrutch = new HumanJaxbCrutch();
        humanJaxbCrutch.setCourses(list.toArray(new Human[list.size()])); 

        return humanJaxbCrutch;
    }

    @Override
    public Map<String, Human> unmarshal(HumanJaxbCrutch humans) throws Exception {
        Map<String, Human> map = new HashMap<String, Human>();

        for(Human human : humans.getCourses()){
            map.put(human.getKey(), human);
        }

        return map;
    }
}

class HumanJaxbCrutch {

    private Human[] courses;

    @XmlElement(name = "human")
    public Human[] getCourses() {
        return courses;
    }

    public void setCourses(Human[] courses) {
        this.courses = courses;
    }
}

My solution

I added the key attribute to human and then I converted the map to an array:

@XmlRootElement
public class World {

    @XmlJavaTypeAdapter(HumanAdapter.class)
    private Map<String, Human> humans = new HashMap<String, Human>();

    public World(){}
}

public class Human {

    @XmlElement
    private String name;

    @XmlAttribute(name="key")
    private String key;

    public Human(){}
}

public class HumanAdapter extends XmlAdapter<HumanJaxbCrutch, Map<String, Human>> {

    @Override
    public HumanJaxbCrutch marshal(Map<String, Human> humans) throws Exception {
        List<Human> list = new ArrayList<Human>();
        for (Entry<String, Human> entry : humans.entrySet()) {
            list.add(entry.getValue());
        }

        HumanJaxbCrutch humanJaxbCrutch = new HumanJaxbCrutch();
        humanJaxbCrutch.setCourses(list.toArray(new Human[list.size()])); 

        return humanJaxbCrutch;
    }

    @Override
    public Map<String, Human> unmarshal(HumanJaxbCrutch humans) throws Exception {
        Map<String, Human> map = new HashMap<String, Human>();

        for(Human human : humans.getCourses()){
            map.put(human.getKey(), human);
        }

        return map;
    }
}

class HumanJaxbCrutch {

    private Human[] courses;

    @XmlElement(name = "human")
    public Human[] getCourses() {
        return courses;
    }

    public void setCourses(Human[] courses) {
        this.courses = courses;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文