简体   繁体   English

JAXB Hashmap的Marshall数组

[英]JAXB Marshall Array of Hashmap

In my current program I currently try to marshall a datastructure in java using JAXB into a xml file. 在当前程序中,我目前尝试使用JAXB将Java中的数据结构编组为xml文件。 An array of hashmap has to be marshalled. 哈希图数组必须被整理。 This does not work completley: While the xml contains x hashmap tags, they are all empty, no matter which content was inside them. 这不能完全完成:尽管xml包含x个hashmap标记,但无论它们中包含什么内容,它们都为空。 For example look at the following classes: 例如,查看以下类:

Class Atlas: 类图集:

@XmlRootElement (name = "atlas")
@XmlAccessorType(XmlAccessType.FIELD)
public class Atlas {

    @XmlElement(name = "file")
    private String[] filePaths;

    private int x, y;

    @XmlElement(name = "objectDefinition")
    private HashMap<Integer, Definition>[] objectDefinitions;

    public void setObjectDefinitions(HashMap<Integer, Definition>[] defs) {
        objectDefinitions = defs;
    }

    public void setFilePaths(String[] paths) {
        filePaths = paths;
    }

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }
}

Class Definition: 类定义:

@XmlAccessorType(XmlAccessType.FIELD)
public class Definition {
    private int a, b;

    public Definition(int a, int b) {
        this.a = a;
        this.b = b;
    }

    public Definition() {}
}

Executing Class: 执行类:

public class XmlLoader {
    public static void execute() {
        Atlas atlas = new Atlas();
        atlas.setX(8);
        atlas.setY(9);
        atlas.setFilePaths(new String[] {
            "path1", "path2"
        });

        Definition def1, def2, def3, def4;
        def1 = new Definition(1,2);
        def2 = new Definition(3,4);
        def3 = new Definition(5,6);
        def4 = new Definition(7,8);
        HashMap<Integer, Definition> map1 = new HashMap<>();
        map1.put(200, def1);
        map1.put(202, def2);
        HashMap<Integer, Definition> map2 = new HashMap<>();
        map2.put(100, def3);
        map2.put(101, def4);

        atlas.setObjectDefinitions(new HashMap[] {
            map1, map2
        });

        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(Atlas.class);
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.marshal(atlas, new File("out.xml"));
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}

This will produce the following output: 这将产生以下输出:

<atlas>
    <file>path1</file>
    <file>path2</file>
    <x>8</x>
    <y>9</y>
    <objectDefinition />
    <objectDefinition />
</atlas>

Which is not what I've expected. 这不是我所期望的。 I would've expected some content inside the 's but it's not there. 我本来希望在内有一些内容,但并不在那里。 If I remove the Array of HashMap (only marshall a single hashmap instead of an array of them), the output of the hashmap is correct. 如果我删除HashMap的数组(仅封送一个哈希表而不是数组),则哈希表的输出是正确的。 I also tried to introduce Wrapper, but it does not work either. 我也尝试介绍了Wrapper,但它也不起作用。

So could you give me a hint what I've to do? 那么,您能给我一个提示吗? Is a custom adapter needed? 是否需要自定义适配器? (And if yes, why?) (如果是,为什么?)

Thanks in advance! 提前致谢!

You need a custom adapter, as JAXB does not naturally map some classes, including HashMap. 您需要一个自定义适配器,因为JAXB自然不会映射某些类,包括HashMap。

Source (First line of the usage part) : https://docs.oracle.com/javase/7/docs/api/javax/xml/bind/annotation/adapters/XmlAdapter.html 来源(用法部分的第一行): https : //docs.oracle.com/javase/7/docs/api/javax/xml/bind/annotation/adapters/XmlAdapter.html

As for what does the adapter, it depends on what kind of XML file structure you wish for. 至于适配器的功能,则取决于所需的XML文件结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM