使用Java XML注解,将多个元素绑定到具有属性键的映射中,JAXB。

3
我有一个XML来源,从中使用JAXB进行反序列化对象。 XML来源:
<album>
    <name>something</name>
    <id>003030</id>
    <artist>someone</artist>
    ...
</album>

Java源代码如下(包含必要的getter/setters):
@XmlRootElement(name="album")
class Album {
    String name;
    Long id;
    String artist;
    ...
}

到目前为止,一切都很顺利。现在我在相册列表中获得了一些不同尺寸的图像URL:
...
<image size="small">http://.../small.jpg</image>
<image size="medium">http://.../medium.jpg</image>
<image size="large">http://.../large.jpg</image>
...

我想将它映射到一个Java Map中,类似于这样:
Map<String,String> imageUrls;

地图的键应该是大小属性,地图的值应该是元素值。 如果可能的话,我应该如何注释这个变量?
1个回答

5

助手类 Pair

@XmlAccessorType(XmlAccessType.FIELD)
public class Pair {

    @XmlAttribute
    private String key;

    @XmlValue
    private String value;

    public Pair() {
    }

    public Pair(String key, String value) {
        this.key = key;
        this.value = value;
    }  
//... getters, setters  
}  

一对的列表

@XmlAccessorType(XmlAccessType.FIELD)
public class PairList 
{
    private List<Pair> values = new ArrayList<Pair>();

    public PairList() {
    }  
//...  
}  

适配器

public class MapAdaptor extends XmlAdapter<PairList, Map<String, String>> 
{
    @Override
    public Map<String, String> unmarshal(PairList list) throws Exception 
    {
        Map<String, String> retVal = new HashMap<String, String>();
        for (Pair keyValue : list.getValues()) 
        {
            retVal.put(keyValue.getKey(), keyValue.getValue());
        }
        return retVal;
    }

    @Override
    public PairList marshal(Map<String, String> map) throws Exception 
    {
        PairList retVal = new PairList();
        for (String key : map.keySet()) 
        {
            retVal.getValues().add(new Pair(key, map.get(key)));
        }
        return retVal;
    }
}

在你的实体中使用

@XmlJavaTypeAdapter(value = MapAdaptor.class)
private Map<String, String> imageUrls = new HashMap<String, String>();  

提示
你可以不使用 PairList 类而是使用 Pair[] 数组来完成它
适配器

public class MapAdaptor extends XmlAdapter<Pair[], Map<String, String>> 
{
    @Override
    public Map<String, String> unmarshal(Pair[] list) throws Exception 
    {
        Map<String, String> retVal = new HashMap<String, String>();
        for (Pair keyValue : Arrays.asList(list)) 
        {
            retVal.put(keyValue.getKey(), keyValue.getValue());
        }
        return retVal;
    }

    @Override
    public Pair[] marshal(Map<String, String> map) throws Exception 
    {
        List<Pair> retVal = new ArrayList<Pair>();
        for (String key : map.keySet()) 
        {
            retVal.add(new Pair(key, map.get(key)));
        }
        return retVal.toArray(new Pair[]{});
    }
}  

但在这种情况下,您无法控制每个对的名称。它将是item,您无法更改它。

<item key="key2">valu2</item>
<item key="key1">valu1</item>  

PS2
如果您尝试使用List<Pair>代替PairList,您将会收到一个Exception异常。

ERROR: java.util.List haven't no-arg constructor

这是一个很好的开始,但你可能想要扩展一下,更正式地定义你想要的确切XML(命名空间、名称),并且我可能会将键作为枚举类型,这样XSD就可以用于静态检查图像大小。 - user268396

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接