我已经解决了 没有XmlAdapter的 问题。
我已经为 Map , Map.Entry 和 Collection 编写了带有JAXB注释的对象。
主要思想是在 xmlizeNestedStructure(…) 方法内部:
看一下代码:
public final class Adapters {private Adapters() {}public static Class<?>[] getXmlClasses() { return new Class<?>[]{ XMap.class, XEntry.class, XCollection.class, XCount.class };}public static Object xmlizeNestedStructure(Object input) { if (input instanceof Map<?, ?>) { return xmlizeNestedMap((Map<?, ?>) input); } if (input instanceof Collection<?>) { return xmlizeNestedCollection((Collection<?>) input); } return input; // non-special object, return as is}public static XMap<?, ?> xmlizeNestedMap(Map<?, ?> input) { XMap<Object, Object> ret = new XMap<Object, Object>(); for (Map.Entry<?, ?> e : input.entrySet()) { ret.add(xmlizeNestedStructure(e.getKey()), xmlizeNestedStructure(e.getValue())); } return ret;}public static XCollection<?> xmlizeNestedCollection(Collection<?> input) { XCollection<Object> ret = new XCollection<Object>(); for (Object entry : input) { ret.add(xmlizeNestedStructure(entry)); } return ret;}@XmlType@XmlRootElementpublic final static class XMap<K, V> { @XmlElementWrapper(name = "map") @XmlElement(name = "entry") private List<XEntry<K, V>> list = new linkedList<XEntry<K, V>>(); public XMap() { } public void add(K key, V value) { list.add(new XEntry<K, V>(key, value)); }}@XmlType@XmlRootElementpublic final static class XEntry<K, V> { @XmlElement private K key; @XmlElement private V value; private XEntry() { } public XEntry(K key, V value) { this.key = key; this.value = value; }}@XmlType@XmlRootElementpublic final static class XCollection<V> { @XmlElementWrapper(name = "list") @XmlElement(name = "entry") private List<V> list = new linkedList<V>(); public XCollection() { } public void add(V obj) { list.add(obj); }}}
有用!
让我们看一个 演示输出 :
<xMap> <map> <entry> <key xsi:type="xCount" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <count>1</count> <content xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">a</content> </key> <value xsi:type="xCollection" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <list> <entry xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">a1</entry> <entry xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">a2</entry> <entry xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">a3</entry> </list> </value> </entry> <entry> <key xsi:type="xCount" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <count>2</count> <content xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">b</content> </key> <value xsi:type="xCollection" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <list> <entry xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">b1</entry> <entry xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">b3</entry> <entry xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">b2</entry> </list> </value> </entry> <entry> <key xsi:type="xCount" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <count>3</count> <content xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">c</content> </key> <value xsi:type="xCollection" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <list> <entry xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">c1</entry> <entry xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">c2</entry> <entry xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">c3</entry> </list> </value> </entry> </map></xMap>
抱歉,演示输出还使用了称为 “ count” 的数据结构,该数据结构 在适配器的源代码中未提及。
顺便说一句: 有谁知道如何删除所有这些烦人的(对于我而言)不必要的 xsi:type 属性?
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)