1
votes

I'm trying to marshal and unmarshal Map to/from value pairs. I can marshal the object successfully, however, I cannot unmarshal it from the xml. The unmarshal result is the key exist in the Map, however, its value is null.

Here's the model I want to marshal and unmarshal:

import java.util.Map;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

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

    @XmlElement(name="Name")
    private String name;

    @XmlJavaTypeAdapter(MapAdapter.class)
    private Map<String, String> metadata;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Map<String, String> getMetadata() {
        return metadata;
    }

    public void setMetadata(Map<String, String> metadata) {
        this.metadata = metadata;
    }
}

I create a Map Adapter for marshal and unmarshal like below:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.namespace.QName;
import org.w3c.dom.Element;

public class MapAdapter extends XmlAdapter<MapWrapper, Map<String, String>> {

@Override
public MapWrapper marshal(Map<String, String> m) throws Exception {
    MapWrapper wrapper = new MapWrapper();
    List<JAXBElement<String>> elements = new ArrayList<JAXBElement<String>>();
    if (m != null && !m.isEmpty()) {
        for (Entry<String, String> property : m.entrySet()) {
            elements.add(new JAXBElement<String>(new QName(property.getKey()), 
                    String.class, property.getValue().toString()));
        }
    }
    wrapper.elements = elements;
    return wrapper;
}

@Override
public Map<String, String> unmarshal(MapWrapper v) throws Exception {
    Map<String, String> map = new HashMap<String, String>();
    if (v != null && v.elements != null && !v.elements.isEmpty()) {
        for (Object object : v.elements) {
            Element element = (Element) object;
            map.put(element.getNodeName(), element.getNodeValue());
        }
    }
    return map;
}

}

class MapWrapper {

    @XmlAnyElement(lax=true)
    protected List<JAXBElement<String>> elements;
}

And the below class can test the marshal and unmarshal of the above model:

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class TestEntry {

public static void main(String[] args) throws JAXBException {
    testMarshal();
    testUnmarshal();
}

public static void testMarshal() throws JAXBException {

    Map<String, String> metadata = new HashMap<String, String>();
    metadata.put("category", "test");
    metadata.put("creation", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
    TestModel model = new TestModel();
    model.setMetadata(metadata);
    model.setName("TESTMODEL");

    marshal(model, System.out);
}

public static void testUnmarshal() throws JAXBException {

    String model = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"
            + "<TestModel>"
            +   "<Name>TESTMODEL</Name>"
            +   "<metadata>"
            +       "<category>test</category>"
            +       "<creation>2015-09-29</creation>"
            +   "</metadata>"
            + "</TestModel>";

    TestModel result = unmarshal(new ByteArrayInputStream(model.getBytes()), TestModel.class);
    System.out.println("name=" + result.getName());
    for (String key : result.getMetadata().keySet()) {
        System.out.println(key + ", " + result.getMetadata().get(key));
    }
}

public static <T> void marshal(T cls, OutputStream os) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(cls.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(cls, os);
}

@SuppressWarnings("unchecked")
public static <T> T unmarshal(InputStream is, Class<T> cls)
        throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(cls);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    return (T) unmarshaller.unmarshal(is);
}
}

It seems that, the map only contains the key with 'null' value when invoke 'public Map unmarshal(MapWrapper v) throws Exception'.

And I also find a similar question about this issue "JAXB: how to marshall map into <key>value</key>", however, it didn't solve the unmarshal issue?

1

1 Answers

1
votes

A very small correction:

map.put(element.getNodeName(), element.getTextContent());