JAXB映射适配器 [英] JAXB Map adapter

查看:111
本文介绍了JAXB映射适配器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将XML复杂类型转换为java.util.Map时遇到了问题。解组时,只会填充值,键变为NULL。我希望下面的XML转换为 java.util.Map<user_type,students> 但是它会像 java.util一样转换.MAP< NULL, 学生 > 。有人可以告诉我为什么我的密钥为NULL?

I've problem in converting XML complex type to java.util.Map. While unmarshalling, only the value is getting populated and key becomes NULL. I want the below XML to be converted as java.util.Map<"user_type","students"> but its coming like java.util.Map<NULL,"students">. Can someone please tell why I'm getting the key as NULL?

XML

<root>
  <myMap>
    <user_type>students</user_type>
  </myMap>
</root>

POJO

@XmlRootElement(name = "root")
@XmlAccessorType(XmlAccessType.FIELD)
public class MyPojo implements Serializable {

private static final long serialVersionUID = -4589166768649033266L;

@XmlElement(name = "myMap")
@XmlJavaTypeAdapter(MapAdapter.class)
private Map<String,String> myMap;

 //getters and setters
}

MapAdapter的实现是 http://blog.bdoughan.com/2013 /06/moxys-xmlvariablenode-using-maps-key-as.html

MapAdapter implementation is http://blog.bdoughan.com/2013/06/moxys-xmlvariablenode-using-maps-key-as.html

@Override
public Map<String, String> unmarshal(AdaptedMap adaptedMap) throws Exception {
    List<AdaptedEntry> adaptedEntries = adaptedMap.entries;
    Map<String, String> map = new HashMap<String, String>(adaptedEntries.size());
    for(AdaptedEntry adaptedEntry : adaptedEntries) {
        map.put(adaptedEntry.key, adaptedEntry.value);
    }
    return map;
}


推荐答案

我看到的第一个错误是您正在使用 Map< String,Integer> 映射,而您的XML具有字符串值。

The first mistake I see is that you are using a Map<String, Integer> mapping whereas your XML has a string value.

更改您的代码如下:

public class MapAdapter extends XmlAdapter<MapAdapter.AdaptedMap, Map<String, String>> {

    public static class AdaptedMap {

        @XmlVariableNode("key")
        List<AdaptedEntry> entries = new ArrayList<AdaptedEntry>();

    }

    public static class AdaptedEntry {

        @XmlTransient
        public String key;

        @XmlValue
        public String value;

    }

    @Override
    public AdaptedMap marshal(Map<String, String> map) throws Exception {
        AdaptedMap adaptedMap = new AdaptedMap();
        for(Entry<String, String> entry : map.entrySet()) {
            AdaptedEntry adaptedEntry = new AdaptedEntry();
            adaptedEntry.key = entry.getKey();
            adaptedEntry.value = entry.getValue();
            adaptedMap.entries.add(adaptedEntry);
        }
        return adaptedMap;
    }

    @Override
    public Map<String, String> unmarshal(AdaptedMap adaptedMap) throws Exception {
        List<AdaptedEntry> adaptedEntries = adaptedMap.entries;
        Map<String, String> map = new HashMap<String, String>(adaptedEntries.size());
        for(AdaptedEntry adaptedEntry : adaptedEntries) {
            map.put(adaptedEntry.key, adaptedEntry.value);
        }
        return map;
    }

}

MyPojo 类,您需要指定需要将哪个XML元素映射为 Map

In your MyPojo class, you need to specify which XML element you will need to map as a Map.

@XmlElement(name = "myMap"),
@XmlJavaTypeAdapter(MapAdapter.class)
private Map<String,String> myMap;

这篇关于JAXB映射适配器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆