I want to convert a HashMap in a POJO class to XML. I tried using the XmlAdapter but it results in only the key and value pairs of the HashMap being the attributes of the XML Elements. I need the Key to be the Element itself and the value of the HashMap to be the value of the element. For instance, I need the following XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cart>
<supervisor_id>555</supervisor_id>
<payments>
<payment sequence="1">
<amount>123.45</amount>
<billing_method>12345</billing_method>
<form>card</form>
<delivery_mode>Q</delivery_mode>
</payment>
<payment sequence="2">
<amount>123.45</amount>
<person_id>2333</person_id>
<form>cash</form>
<delivery_mode>Q</delivery_mode>
</payment>
</payments>
</cart>
I created the following classes: MyMapType holds a list of MyMapEntryType class which has two fields namely Key and Value. How do I change the Key element to be @XmlElement and assign the value field to the Key field?
Here are my source files.
MyMapType.java
import java.util.ArrayList;
import java.util.List;
public class MyMapType {
private List<MyMapEntryType> entry = new ArrayList<MyMapEntryType>();
public List<MyMapEntryType> getEntry() {
return entry;
}
public void setEntry(List<MyMapEntryType> entry) {
this.entry = entry;
}
}
MyMapEntryType.java
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;
@XmlAccessorType(XmlAccessType.FIELD)
public class MyMapEntryType {
@XmlAttribute
private String key;
@XmlValue
private String value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
Please also find the adapter class:
MyMapAdapter.java
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class MyMapAdapter extends XmlAdapter<MyMapType, Map<String, String>> {
@Override
public MyMapType marshal(Map<String, String> map) throws Exception {
MyMapType myMapType = new MyMapType();
for(Entry<String, String> entry : map.entrySet()) {
MyMapEntryType myMapEntryType = new MyMapEntryType();
myMapEntryType.setKey(entry.getKey());
myMapEntryType.setValue(entry.getValue());
myMapType.getEntry().add(myMapEntryType);
}
return myMapType;
}
@Override
public Map<String, String> unmarshal(MyMapType map) throws Exception {
HashMap<String, String> hashMap = new HashMap<String, String>();
for(MyMapEntryType myEntryType : map.getEntry()) {
hashMap.put(myEntryType.getKey(), myEntryType.getValue());
}
return hashMap;
}
}
This is the class which has the HashMap field:
XmlElementMap.java
@XmlAccessorType(XmlAccessType.FIELD)
public class XmlElementMap {
@XmlAttribute(name="sequence")
private int sequence;
@XmlJavaTypeAdapter(MyMapAdapter.class)
private Map<String, String> map = new HashMap<String, String>();
public int getSequence() {
return sequence;
}
public void setSequence(int sequence) {
this.sequence = sequence;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
}
Please advise on how to achieve this.
Regards,
-Anand
Currently it produces the following output:
unmarshalling
. I am trying tounmarshal
unknown elements intoMap<String,Object>
but it's not working as expected. I have posted the complete question in the below-provided link. If you get a chance can you please have a look at it and provide your suggestion: #67649441 – Greenbelt