my application works well in java 6, but after I updated java to version 7, some tests failed. through debugging, I found that jaxb was not able to parse the XML file correctly using java 7. here is my code:
xml file for test:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ma:recommendations xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ma="http://schemas.hyi.com/service/1.0"><responseTime>17</responseTime><requestId>MRECS-123a40a9-9cbe-4580-88a2-d88e86abcac8</requestId> <ma:recommendation> <id>pop1-a</id> <type></type> <name></name> <category></category> <rating>1.0</rating> <price></price> <currency></currency> <iconURI></iconURI> <downloadURI></downloadURI> <ratingCount></ratingCount> <downloadCount></downloadCount> <ma:extensions> <ma:extension ma:key="type"> <ma:value>foo</ma:value> </ma:extension> </ma:extensions> </ma:recommendation> </ma:recommendations>
jaxb cannot parse the extesions tag correctly, it always returns null for extensions.
Codes: RecommendedItem.java
@XmlRootElement(namespace = XMLNamespace.URL) @XmlAccessorType(XmlAccessType.NONE) @XmlType() public final class RecommendedItem implements Serializable { public static final String ITEM_TYPE_EXTENSION = "type"; .... @XmlElement(name = "extensions", namespace = XMLNamespace.URL, required = false) private ExtensionMap _extensions; ...
ExtensionMap.java
@XmlRootElement(namespace = XMLNamespace.URL) @XmlAccessorType(XmlAccessType.NONE) @XmlType(namespace = XMLNamespace.URL) public final class ExtensionMap extends HashMap<String, String> { private static final long serialVersionUID = -7235844731135521813L; public ExtensionMap() { super(); } public ExtensionMap(int capacity) { super(capacity); } @XmlElement(name = "extension", namespace = XMLNamespace.URL, required = false) @SuppressWarnings("unused") private List<ExtensionMapElement> getEntries() { return new ListAdapter(); } @SuppressWarnings("unused") private void setEntries(List<ExtensionMapElement> entries) { if (entries != null) { for (ExtensionMapElement entry : entries) { put(entry.getKey(), entry.getValue()); } } } private class ListAdapter implements List<ExtensionMapElement> { @Override public boolean add(ExtensionMapElement e) { put(e.getKey(), e.getValue()); return true; } @Override public void add(int index, ExtensionMapElement element) { add(element); } @Override public boolean addAll(Collection<? extends ExtensionMapElement> c) { if (c != null) { for (ExtensionMapElement element : c) { add(element); } } return true; } @Override public boolean addAll(int index, Collection<? extends ExtensionMapElement> c) { addAll(c); return true; } @Override public void clear() { ExtensionMap.this.clear(); } @Override public boolean contains(Object o) { throw new UnsupportedOperationException("Not supported yet."); } @Override public boolean containsAll(Collection<?> c) { throw new UnsupportedOperationException("Not supported yet."); } @Override public ExtensionMapElement get(int index) { throw new UnsupportedOperationException("Not supported yet."); } @Override public int indexOf(Object o) { throw new UnsupportedOperationException("Not supported yet."); } @Override public boolean isEmpty() { return ExtensionMap.this.isEmpty(); } @Override public Iterator<ExtensionMapElement> iterator() { return new Iterator<ExtensionMapElement>() { private Iterator<Map.Entry<String, String>> _iter = ExtensionMap.this.entrySet().iterator(); @Override public boolean hasNext() { return _iter.hasNext(); } @Override public ExtensionMapElement next() { return new ExtensionMapElement(_iter.next()); } @Override public void remove() { throw new UnsupportedOperationException("Not supported yet."); } }; } @Override public int lastIndexOf(Object o) { throw new UnsupportedOperationException("Not supported yet."); } @Override public ListIterator<ExtensionMapElement> listIterator() { throw new UnsupportedOperationException("Not supported yet."); } @Override public ListIterator<ExtensionMapElement> listIterator(int index) { throw new UnsupportedOperationException("Not supported yet."); } @Override public boolean remove(Object o) { throw new UnsupportedOperationException("Not supported yet."); } @Override public ExtensionMapElement remove(int index) { throw new UnsupportedOperationException("Not supported yet."); } @Override public boolean removeAll(Collection<?> c) { throw new UnsupportedOperationException("Not supported yet."); } @Override public boolean retainAll(Collection<?> c) { throw new UnsupportedOperationException("Not supported yet."); } @Override public ExtensionMapElement set(int index, ExtensionMapElement element) { add(element); return null; } @Override public int size() { return ExtensionMap.this.size(); } @Override public List<ExtensionMapElement> subList(int fromIndex, int toIndex) { throw new UnsupportedOperationException("Not supported yet."); } @Override public Object[] toArray() { throw new UnsupportedOperationException("Not supported yet."); } @Override public <T> T[] toArray(T[] a) { throw new UnsupportedOperationException("Not supported yet."); } } } @XmlRootElement(namespace = XMLNamespace.URL) @XmlAccessorType(XmlAccessType.FIELD) @XmlType(namespace = XMLNamespace.URL) final class ExtensionMapElement implements Serializable { private static final long serialVersionUID = 8211130122512683829L; @XmlAttribute(name = "key", namespace = XMLNamespace.URL, required = true) private String _key; @XmlElement(name = "value", namespace = XMLNamespace.URL, required = true) private String _value; public ExtensionMapElement() { } public ExtensionMapElement(Map.Entry<String, String> e) { _key = e.getKey(); _value = e.getValue(); } public String getKey() { return _key; } public String getValue() { return _value; } }
please, help. thanks.
source share