hallo zusammen,
ich habe nachfolgendes Problem:
daraus resultiert folgende XML:
ich möchte aber folgende xml-Darstellung
Das Attribute "key" aus der Klasse MyMapEntryType (key der HashMap) bzw. der Wert des Attributes soll selbst als XML-Element verwendet werden. Wie geht das?
Danke!!
ich habe nachfolgendes Problem:
Code:
@XmlRootElement(name = "task") public class XmlTaskConfig implements Serializable { private String taskName; private Map<String, String> params; public XmlTaskConfig() {} @XmlElement(name = "name") public String getTaskName() { return taskName; } @XmlJavaTypeAdapter(MapAdapter.class) @XmlElement(name="params") public void setParams(Map<String, String> params) { this.params = params; } } public class MapAdapter extends XmlAdapter<MyMapEntryType[], Map<String, String>> { @Override public Map<String, String> unmarshal(MyMapEntryType[] arg0) throws Exception { HashMap<String, String> hashMap = new HashMap<String, String>(); for(MyMapEntryType test : arg0) { hashMap.put(test.key, test.value); } return hashMap; } @Override public MyMapEntryType[] marshal(Map<String, String> arg0) throws Exception { MyMapEntryType[] myMapType = new MyMapEntryType[arg0.size()]; int i=0; for(Entry<String, String> entry : arg0.entrySet()) { myMapType[i] = new MyMapEntryType(entry.getKey(), entry.getValue()); i++; } return myMapType; } } public class MyMapEntryType { @XmlAttribute public String key; @XmlValue public String value; private MyMapEntryType() { //Required by JAXB } public MyMapEntryType(String key, String value) { this.key = key; this.value = value; } }
daraus resultiert folgende XML:
Code:
<task> <name>Task 2</name> <params> <item key="attribute1">value1</item> <item key="attribute2">value2</item> <item key="attribute3">value3</item> <item key="attribute4">value4</item> <item key="attribute5">value5</item> <item key="attribute6">value6</item> <item key="attribute7">value7</item> </params> </task>
ich möchte aber folgende xml-Darstellung
Code:
<task> <name>Task 2</name> <params> <attribute1>value1</attribute1> <attribute2>value2</attribute2> <attribute3>value3</attribute3> <attribute4>value4</attribute4> <attribute5>value5</attribute5> <attribute6>value6</attribute6> <attribute7>value7</attribute7> </params> </task>
Das Attribute "key" aus der Klasse MyMapEntryType (key der HashMap) bzw. der Wert des Attributes soll selbst als XML-Element verwendet werden. Wie geht das?
Danke!!
Comment