我的应用程序在java 6中运行良好,但在我将java更新到版本7之后,某些测试失败了。 通过调试我发现jaxb无法使用java 7正确解析某些xml文件。这是我的代码:

用于测试的xml文件:

<?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无法再正确解析extesions标记,它总是为扩展返回null。

代码在这里:RecommendedItem .java

@XmlRootElement(namespace = XMLNamespace.URL)
@XmlAccessorType(XmlAccessType.NONE)
@XmlType()
public final class RecommendedItem implements Serializable {

    /**
     * Item type extension field name.
     */
    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> {
    /** Serialized version unique identifier. */
    private static final long serialVersionUID = -7235844731135521813L;

    /**
     * Default constructor to support JAXB binding.
     */
    public ExtensionMap() {
        super();
    }
    /**
     * Default constructor to support JAXB binding.
     * @param capacity The expected capacity of the map.
     */
    public ExtensionMap(int capacity) {
        super(capacity);
    }

    /**
     * The list of wrapped Map entries that are structured for binding to XML
     * with JAXB.
     * 
     * @return The list of wrapped Map entries that are structured for binding
     *         to XML with JAXB.
     */
    @XmlElement(name = "extension", namespace = XMLNamespace.URL, required = false)
    @SuppressWarnings("unused")
    private List<ExtensionMapElement> getEntries() {
        return new ListAdapter();
    }
    /**
     * The list of wrapped Map entries that are structured for binding to XML
     * with JAXB.
     * 
     * @param entries The list of wrapped Map entries that are structured for binding
     *         to XML with JAXB.
     */
    @SuppressWarnings("unused")
    private void setEntries(List<ExtensionMapElement> entries) {
        if (entries != null) {
            for (ExtensionMapElement entry : entries) {
                put(entry.getKey(), entry.getValue());
            }
        }
    }


    /**
     * Adapter for the list collection.
     * 
     */
    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.");
        }
    }
}
/**
 * A utility type that wraps map entries to support a mapping between a
 * {@link java.util.Map} interface and a class that can be bound to XML using
 * JAXB.
 */
@XmlRootElement(namespace = XMLNamespace.URL)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = XMLNamespace.URL)
final class ExtensionMapElement implements Serializable {

    /**
     * Serialized version unique identifier.
     */
    private static final long serialVersionUID = 8211130122512683829L;

    /**
     * The key of the wrapped map entry.
     */
    @XmlAttribute(name = "key", namespace = XMLNamespace.URL, required = true)
    private String _key;

    /**
     * The value of the wrapped map entry.
     */
    @XmlElement(name = "value", namespace = XMLNamespace.URL, required = true)
    private String _value;

    /**
     * Default constructor to support JAXB Binding.
     */
    public ExtensionMapElement() {
    }

    /**
     * Wraps a map entry with an instance of this class.
     * 
     * @param e
     *            The map entry to wrap.
     */
    public ExtensionMapElement(Map.Entry<String, String> e) {
        _key = e.getKey();
        _value = e.getValue();
    }

    /**
     * The key of the wrapped map entry.
     * 
     * @return The key of the wrapped map entry.
     */
    public String getKey() {
        return _key;
    }

    /**
     * The value of the wrapped map entry.
     * 
     * @return The value of the wrapped map entry.
     */
    public String getValue() {
        return _value;
    }
}

请帮忙。 谢谢。

  ask by user468587 translate from so

本文未有回复,本站智能推荐: