1

I want to unmarshal the following xml using EclipseLink JAXB (MOXy)

<MyObject>
    <Value1>1</Value1>
    <Value2>2</Value2>
    ...
    <ValueN>N</ValueN>
    <Items>
        <Item>
            <Value4>4</Value4>
            ...
            <ValueM>M</ValueM>
        </Item>
        ...
    </Items>
</MyObject>

Where nodes Value1, Value2, etc, are variables.

For this I use the following class

@XmlRootElement(name="MyObject")
public class MyObject {

    public MyObject() {
        this.items = new ArrayList<Item>();
    }

    @XmlPath(".")
    @XmlJavaTypeAdapter(MyMapAdapter.class)
    private Map<String, String> map = new HashMap<String, String>();

    private List<Item> items;

    public List<Item> getItems() {
        return items;
    }

    @XmlElementWrapper(name="Items")
    @XmlElement(name="Item")
    public void setItems(List<Item> items) {
        this.items = items;
    }
}

Where in MyMapAdapter I get a list of Value nodes and their "values". If I remove the "map" property, then the values of the list are loaded correctly.

This is MyMapAdapter:

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) {
        if(!"Items".equals(adaptedEntry.key)){
            map.put(adaptedEntry.key, adaptedEntry.value);
        }
    }

    return map;
}

}

What am I doing wrong? How this can be done?

Any help will be greatly appriciated

Mario Gomez
  • 139
  • 1
  • 9
  • show your `MyMapAdapter` – guido Nov 24 '14 at 17:46
  • thanks; are you trying to adapt the values elements inside the `Items`, or the values elements inside `MyObject`? – guido Nov 24 '14 at 19:49
  • I think the problem is in MyObject, because if I remove the "map" property, then the values of the list are loaded correctly. – Mario Gomez Nov 24 '14 at 21:20
  • mmh i see; @BlaiseDoughan is probably the right person to get help for this – guido Nov 24 '14 at 22:46
  • I still can't find the solution, @BlaiseDoughan could you help me? – Mario Gomez Nov 28 '14 at 13:09
  • @guido I know it's quite old but even I'm stuck in this issue lately. Do you know some resolution or workaround to resolve the issue? Following are my observations: I tried to debug the `Moxy` code but still no luck in finding the solution. But I got to know that the issue is happening due to the `annotation @XmlPath(".")`. As we have given the `(".")` all the fields will be `unmarshalled` into the `Map`. I guess this is the intended workflow for `Moxy` so not a bug. I tried a lot of workarounds but no luck. My post here: https://stackoverflow.com/q/67648941/7584240 – BATMAN_2008 Jun 01 '21 at 18:21

0 Answers0