3
votes

I have an XML source from which I unmarshall Objects with JAXB. The XML source:

<album>
    <name>something</name>
    <id>003030</id>
    <artist>someone</artist>
    ...
</album>

The java source is like (with the required getter/setters as well):

@XmlRootElement(name="album")
class Album {
    String name;
    Long id;
    String artist;
    ...
}

So far so good. Now I get some image urls in different sizes within album list:

...
<image size="small">http://.../small.jpg</image>
<image size="medium">http://.../medium.jpg</image>
<image size="large">http://.../large.jpg</image>
...

I want to map it to a java Map something like this:

Map<String,String> imageUrls;

Where the map's key would be the size attribute and the map's value would be the element value. If it's possible, how should I annotate this variable?

1

1 Answers

5
votes

helper class 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  
}  

List of pairs

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

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

adaptor

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;
    }
}

usage in your entity

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

PS
You can do it without class PairList using Pair[] instead of PairList
adaptor

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[]{});
    }
}  

but in this case you can't control name of every pair. It will be item and you can't change it

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

PS2
If you will try use List<Pair> instead of PairList, you will get Exception

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