java - JAXB 编码由 XmlAdapter 创建的 ArrayList

标签 java arraylist jaxb marshalling xmladapter

我想使用 XmlAdapter 调整 HashMap 字段的 XML 表示。我使用 ArrayList 来做到这一点。但是,在编码时,ArrayList 根本没有编码。这是为什么?

代码

@XmlRootElement
public class Foo {

    private HashMap<String, String> hashMap;

    public Foo() {
        this.hashMap = new HashMap<String, String>();
    }

    @XmlJavaTypeAdapter(HashMapAdapter.class)
    public HashMap<String, String> getHashmap() {
        return hashMap;
    }

    public void setHashmap(HashMap<String, String> hashMap) {
        this.hashMap = hashMap;
    }

}
public final class HashMapAdapter extends XmlAdapter<ArrayList<HashMapEntry>, HashMap<String, String>> {

    @Override
    public ArrayList<HashMapEntry> marshal(HashMap<String, String> arg0) throws Exception {
        ArrayList<HashMapEntry> result = new ArrayList<HashMapEntry>();
        for(Entry<String, String> entry : arg0.entrySet())
            result.add(new HashMapEntry(entry.getKey(), entry.getValue()));
        return result;
    }

    @Override
    public HashMap<String, String> unmarshal(ArrayList<HashMapEntry> arg0) throws Exception {
        HashMap<String, String> result = new HashMap<String, String>();
        for(HashMapEntry entry : arg0)
            result.put(entry.key, entry.value);
        return result;
    }

}
public class HashMapEntry {

    @XmlElement 
    public String key;

    @XmlValue
    public String value;

    public HashMapEntry() {

    }

    public HashMapEntry(String key, String value) {
        this.key = key;
        this.value = value;
    }
}

结果

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><foo><hashmap/></foo>

最佳答案

在您的 XmlAdapter 中,您需要将 HashMap 转换为具有 List 属性的对象实例,而不是直接转换为 ArrayList.

HashMap 适配器

package forum13163430;

import java.util.*;
import java.util.Map.Entry;

import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public final class HashMapAdapter extends XmlAdapter<HashMapAdapter.AdaptedHashMap, HashMap<String, String>> {

    @Override
    public AdaptedHashMap marshal(HashMap<String, String> hashMap) throws Exception {
        AdaptedHashMap adaptedHashMap = new AdaptedHashMap();
        for(Entry<String, String> entry : hashMap.entrySet()) {
            adaptedHashMap.item.add(new HashMapEntry(entry.getKey(), entry.getValue()));
        }
        return adaptedHashMap;
    }

    @Override
    public HashMap<String, String> unmarshal(AdaptedHashMap adaptedHashMap) throws Exception {
        HashMap<String, String> result = new HashMap<String, String>();
        for(HashMapEntry entry : adaptedHashMap.item)
            result.put(entry.key, entry.value);
        return result;
    }

    public static class AdaptedHashMap {
        public List<HashMapEntry> item = new ArrayList<HashMapEntry>();
    }

    public static class HashMapEntry {

        @XmlAttribute 
        public String key;

        @XmlValue
        public String value;

        public HashMapEntry() {
        }

        public HashMapEntry(String key, String value) {
            this.key = key;
            this.value = value;
        }
    }

}

了解更多信息


更新

Thanks, this works. However then I get an additional level of annotation in the produced XML. Is there any way to avoid that?

如果您使用 EclipseLink MOXy作为你的JAXB (JSR-222) provider 然后你可以利用这个用例的 @XmlPath 扩展。我将在下面举例说明。

除了 @XmlJavaTypeAdapter 之外,我还在 hashmap 属性上添加了 MOXy 的 @XmlPath 注释。 “.” 的 XML 路径表示应将子元素编码到父 XML 元素中。

package forum13163430;

import java.util.HashMap;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement
public class Foo {

    private HashMap<String, String> hashMap;

    public Foo() {
        this.hashMap = new HashMap<String, String>();
    }

    @XmlPath(".")
    @XmlJavaTypeAdapter(HashMapAdapter.class)
    public HashMap<String, String> getHashmap() {
        return hashMap;
    }

    public void setHashmap(HashMap<String, String> hashMap) {
        this.hashMap = hashMap;
    }

}

jaxb.properties

要将 MOXy 指定为您的 JAXB 提供程序,您需要在与域模型相同的包中包含一个名为 jaxb.properties 的文件,其中包含以下条目(请参阅:http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html)。

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

演示

由于 MOXy 是 JAXB (JSR-222) 兼容实现,标准 API 可用于将对象从 XML 转换为 XML。

package forum13163430;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Foo.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum13163430/input.xml");
        Foo foo = (Foo) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(foo, System.out);
    }

}

input.xml/输出

下面是运行演示代码的输入和输出。

<?xml version="1.0" encoding="UTF-8"?>
<foo>
   <item key="b">B</item>
   <item key="c">C</item>
   <item key="a">A</item>
</foo>

了解更多信息

关于java - JAXB 编码由 XmlAdapter 创建的 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13163430/

相关文章:

java - jackson 与 jaxb

eclipse - 我怎样才能治愈... cvc-elt.1 : Cannot find the declaration of element 'jaxb:bindings'

java - GLSL 着色器无法在 Android 上编译

java - 在Java中将大约80000条大数据插入postgres数据库失败

Java:混合实现类

Java:在随机播放中排除数组索引

java - 将原生 uint8_t(Java 字节)转换为 int

java - 在 Java 中获取列表中不存在于另一个列表中的下一个项目

具有 2 列的 Java ArrayList

java - JAXB - 从 url 解码