I’m trying to implement this, where
<SomeXml> <SomeData>...</SomeData> <InputData> <Param key="key1" value="value1" /> <Param key="key2" value="value2" /> </InputData> <OutputData> <Param key="key3" value="value3" /> </OutputData> </SomeXml>
becomes
public class SomeXml { private SomeData someData; private Map<String, String> inputData; private Map<String, String> outputData; }
Where the inputData map has (key1, value1), (key2, value2) and the outputData map has (key3, value3).
Here is what I have written;
@NoArgsConstructor @AllArgsConstructor @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement public class MapElement { @XmlAttribute(name = "key') private String key; @XmlAttribute(name = "value") private String value; } @NoArgsConstructor public class MapAdapter extends XmlAdapter<MapElement[], Map<String, String>> { public MapElement[] marshal(Map<String, String> args) throws Exception { MapElement[] mapElements = new mapElement[args.size()]; int i = 0; for (Map.Entry<String, String> entry : args.entrySet()) { mapElements[i++] = new MapElement(entry.getKey(), entry.getValue()); } return mapElements; } public Map<String, String> unmarshal(MapElement[] args) throws Exception { Map<String, String> m = new TreeMap<>(); for (MapElement elem : args) { m.put(elem.getKey(), elem.getValue()); } return m; } } @NoArgsConstructor @AllArgsConstructor @XmlAccessorType(XmlAccessorType.FIELD) @XmlRootElement public class SomeXml { @XmlElement private SomeData someData; @XmlJavaAdapter(MapAdapter.class) @XmlElement(name = "InputData") private Map<String, String> inputData; @XmlJavaAdapter(MapAdapter.class) @XmlElement(name = "OutputData") private Map<String, String> outputData; }
From what I have managed to determine, the InputData and OutputData maps are nonnull, so they are being created, but when checking the length of the args of the MapAdapter.unmarshal function, it is zero, implying that I’m not able to read in the tagged information properly. Any help would be appreciated.
Anonymous Asked question May 14, 2021
Recent Comments