JAXB HashMap无法映射
我想将POJO类中的HashMap转换为XML。 我尝试使用XmlAdapter但它只导致HashMap的键和值对是XML Elements的属性。 我需要将Key作为Element本身,并将HashMap的值作为元素的值。 例如,我需要以下XML:
555 123.45 12345 Q 123.45 2333 Q
我创建了以下类:MyMapType包含MyMapEntryType类的列表,该类包含两个字段,即Key和Value。 如何将Key元素更改为@XmlElement并将值字段分配给Key字段?
这是我的源文件。
MyMapType.java
import java.util.ArrayList; import java.util.List; public class MyMapType { private List entry = new ArrayList(); public List getEntry() { return entry; } public void setEntry(List 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; } }
还请找到适配器类:
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> { @Override public MyMapType marshal(Map map) throws Exception { MyMapType myMapType = new MyMapType(); for(Entry entry : map.entrySet()) { MyMapEntryType myMapEntryType = new MyMapEntryType(); myMapEntryType.setKey(entry.getKey()); myMapEntryType.setValue(entry.getValue()); myMapType.getEntry().add(myMapEntryType); } return myMapType; } @Override public Map unmarshal(MyMapType map) throws Exception { HashMap hashMap = new HashMap(); for(MyMapEntryType myEntryType : map.getEntry()) { hashMap.put(myEntryType.getKey(), myEntryType.getValue()); } return hashMap; } }
这是具有HashMap字段的类:
XmlElementMap.java
@XmlAccessorType(XmlAccessType.FIELD) public class XmlElementMap { @XmlAttribute(name="sequence") private int sequence; @XmlJavaTypeAdapter(MyMapAdapter.class) private Map map = new HashMap(); public int getSequence() { return sequence; } public void setSequence(int sequence) { this.sequence = sequence; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } }
请告知如何实现这一目标。
问候,
-Anand
目前它产生以下输出:
我有相同的要求“我需要Key作为元素本身,HashMap的值是元素的值”。
我没有使用自定义适配器,但是通过将HashMap条目动态转换为JAXBElement对象列表来实现它,然后使用@XmlAnyElement对列表进行注释。
@XmlRootElement(name="root") public class MyMapType { @XmlAnyElement public List entries = new ArrayList (); public MyMapType() { // JAXB required } public MyMapType(Map map) { for (Map.Entry entry : map.entrySet()) { entries.add(new JAXBElement(new QName(entry.getKey()), String.class, entry.getValue())); } } public static void main(String[] args) throws Exception { JAXBContext context = JAXBContext.newInstance(MyMapType.class); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); Map map = new LinkedHashMap(); map.put("key1", "value1"); map.put("key2", "value2"); MyMapType mt = new MyMapType(map); marshaller.marshal(mt, System.out); } }
输出是,
value1 value2
注意:可以在此处找到Map
实例的marshal / unmarshal示例: 使用JAXB的动态标记名称 。