Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within XML
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
XML and Related Technologies
JAXB - HashMap
Matthias Eichner
Greenhorn
Posts: 2
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi,
my goal is to marshall a HashMap to XML with JAXB. So I wrote the following code:
@XmlRootElement(name ="root") public class HashMapTest { @XmlJavaTypeAdapter(HashMapAdapter.class) public Map<String, String> map = new HashMap<String, String>(); public static class HashMapAdapter extends XmlAdapter<MapType, Map<String, String>> { @Override public MapType marshal(Map<String, String> map) { MapType mapType = new MapType(); for (Entry<String, String> entry : map.entrySet()) { MapEntry mapEntry = new MapEntry(); mapEntry.key = entry.getKey(); mapEntry.value = entry.getValue(); mapType.entryList.add(mapEntry); } return mapType; } @Override public Map<String, String> unmarshal(MapType type) throws Exception { Map<String, String> map = new HashMap<String, String>(); for (MapEntry entry : type.entryList) { map.put(entry.key, entry.value); } return map; } } public static class MapType { @XmlElement(name ="entry") public List<MapEntry> entryList = new ArrayList<MapEntry>(); } public static class MapEntry { @XmlAttribute public String key; @XmlValue public String value; } public static void main(String args[]) throws Exception { HashMapTest mp = new HashMapTest(); mp.map.put("key1", "value1"); mp.map.put("key2", "value2"); JAXBContext jc = JAXBContext.newInstance(HashMapTest.class); Marshaller m = jc.createMarshaller(); m.marshal(mp, System.out); } }
The result is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <root> <map> <entry key="key2">value2</entry> <entry key="key1">value1</entry> </map> </root>
Looks good, but I dont want the map-Element. Searched everywhere but find nothing to get rid of it.
This is my goal:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <root> <entry key="key2">value2</entry> <entry key="key1">value1</entry> </root>
Hope anyone can help me.
Thanks in advance
Mewel
Matthias Eichner
Greenhorn
Posts: 2
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Found a solution:
@XmlRootElement(name = "root") @XmlAccessorType(XmlAccessType.NONE) public class HashMapTest2 { public Map<String, String> map = new HashMap<String, String>(); @XmlElement(name = "entry") public MapEntry[] getMap() { List<MapEntry> list = new ArrayList<MapEntry>(); for (Entry<String, String> entry : map.entrySet()) { MapEntry mapEntry =new MapEntry(); mapEntry.key = entry.getKey(); mapEntry.value = entry.getValue(); list.add(mapEntry); } return list.toArray(new MapEntry[list.size()]); } public void setMap(MapEntry[] arr) { for(MapEntry entry : arr) { this.map.put(entry.key, entry.value); } } public static class MapEntry { @XmlAttribute public String key; @XmlValue public String value; } public static void main(String args[]) throws Exception { HashMapTest2 mp = new HashMapTest2(); mp.map.put("key1", "value1"); mp.map.put("key2", "value2"); JAXBContext jc = JAXBContext.newInstance(HashMapTest2.class); Marshaller m = jc.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.marshal(mp, System.out); Unmarshaller u = jc.createUnmarshaller(); StringBuffer xmlStr = new StringBuffer( "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><root><entry key=\"key2\">value2</entry><entry key=\"key1\">value1</entry></root>"); HashMapTest2 mp2 = (HashMapTest2)u.unmarshal( new StreamSource( new StringReader( xmlStr.toString() ) ) ); m.marshal(mp2, System.out); } }
It wasn't my idea to go to some crazy nightclub in the middle of nowhere. I just wanted to stay home and cuddle with this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
HashMap sorting
Hibernate mapping HashMap to row
Hashmap, retrieving value and re-storing with different key
NullPointerException
HashMap Construction with initial values
More...