Java tutorial
/******************************************************************************* * Copyright (c) 2005, 2014 springside.github.io * * Licensed under the Apache License, Version 2.0 (the "License"); *******************************************************************************/ package com.ppcxy.cyfm.showcase.demos.utilities.xml; import com.google.common.collect.Maps; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlType; import javax.xml.bind.annotation.XmlValue; import javax.xml.bind.annotation.adapters.XmlAdapter; import java.util.ArrayList; import java.util.List; import java.util.Map; /** * Map<String,String> houses?xml, * Adapter--HouseMapAdapter, List<HouseEntry> Wrapper--HouseMap, MapEntry--HouseEntry. * ?? * * <pre> * <houses> * <house key="bj">house1</item> * <hosue key="gz">house2</item> * </houses> * </pre> * * @author calvin */ public class HouseMapAdapter extends XmlAdapter<HouseMapAdapter.HouseMap, Map<String, String>> { @Override public HouseMap marshal(Map<String, String> map) throws Exception { HouseMap houseMap = new HouseMap(); for (Map.Entry<String, String> e : map.entrySet()) { houseMap.entries.add(new HouseMap.HouseEntry(e)); } return houseMap; } @Override public Map<String, String> unmarshal(HouseMap houseMap) throws Exception { Map<String, String> map = Maps.newLinkedHashMap(); for (HouseMap.HouseEntry e : houseMap.entries) { map.put(e.key, e.value); } return map; } /** * List<HouseEntry>Adapter. * * @author calvin */ @XmlType(name = "houses") public static class HouseMap { @XmlElement(name = "house") List<HouseEntry> entries = new ArrayList<HouseEntry>(); /** * HouseMapEntry. */ static class HouseEntry { @XmlAttribute String key; @XmlValue String value; public HouseEntry() { } public HouseEntry(Map.Entry<String, String> e) { key = e.getKey(); value = e.getValue(); } } } }