JAXB: how to marshall map into <key>value</key>
2013-12-06 10:48
387 查看
MapElements :
MapAdapter :
The rootElement :
I found the code in this website : http://www.developpez.net/forums/d972324/java/general-java/xml/hashmap-jaxb/
package com.cellfish.mediadb.rest.lucene; import javax.xml.bind.annotation.XmlElement; class MapElements { @XmlElement public String key; @XmlElement public Integer value; private MapElements() {} //Required by JAXB public MapElements(String key, Integer value) { this.key = key; this.value = value; } }
MapAdapter :
import java.util.HashMap; import java.util.Map; import javax.xml.bind.annotation.adapters.XmlAdapter; class MapAdapter extends XmlAdapter<MapElements[], Map<String, Integer>> { public MapElements[] marshal(Map<String, Integer> arg0) throws Exception { MapElements[] mapElements = new MapElements[arg0.size()]; int i = 0; for (Map.Entry<String, Integer> entry : arg0.entrySet()) mapElements[i++] = new MapElements(entry.getKey(), entry.getValue()); return mapElements; } public Map<String, Integer> unmarshal(MapElements[] arg0) throws Exception { Map<String, Integer> r = new HashMap<String, Integer>(); for (MapElements mapelement : arg0) r.put(mapelement.key, mapelement.value); return r; } }
The rootElement :
import java.util.HashMap; import java.util.Map; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; @XmlRootElement public class Root { private Map<String, Integer> mapProperty; public Root() { mapProperty = new HashMap<String, Integer>(); } @XmlJavaTypeAdapter(MapAdapter.class) public Map<String, Integer> getMapProperty() { return mapProperty; } public void setMapProperty(Map<String, Integer> map) { this.mapProperty = map; } }
I found the code in this website : http://www.developpez.net/forums/d972324/java/general-java/xml/hashmap-jaxb/
相关文章推荐
- JAXB: how to marshall map into <key>value</key>
- How to sort a Map<Key, Value> on the values in Java?
- 在hibernate中 以Collection<Entity> 替换Map<Key, List<Value>结构
- Sort a Map<Key, Value> by values (Java)
- 【Stackoverflow好问题】Map<Key,Value>基于Value值排序
- ctypes.ArgumentError: argument 1: <class 'TypeError'>: Don't know how to convert parameter 1
- How to fix "Resource file <C_PsdRsdll> not found" issue in Windows OS
- Note On <Programming Entity Framework 2nd Edition> -01: How to Use LINQPad 4.5
- Don't know how to iterate over supplied "items" in &lt;forEach&gt;解决办法
- <c:set var="root" value="${pageContext.request.contextPath}"></c:set>According to TLD or attribute d
- How to include <android/bitmap.h> and other *.h NDK libraries to Titanium Android module?
- Don't know how to iterate over supplied "items" in <forEach>
- Hadoop Writable深度复制及读取任意<key,value>序列文件
- Spring 注解 hibernate 实体方法 <property name="packagesToScan" value="com.sise.domain"/>
- Don't know how to iterate over supplied "items" in <forEach>
- iOS [<NSObject 0x170204110> setValue:forUndefineKey:]: this class is not key value coding-compliant
- List<Map>集合根据Map里某个key对List集合重新排序介绍
- 控制台app.config 的 <add key="aspnet:MaxHttpCollectionKeys" value="10000" />无效
- !!![YOU SHOULD NEVER EVER BE SO FOOL TO MAP <C-N> TO <C-X><C-O>]!!!
- [阅读] 偶尔一本经典书:<How to solve it>by G. POLYA