0

我有一个以前在 java 6 中工作的 jaxb 代码,但是在我更新到 java 7 之后,它生成了不同的 xml 文件。

在 java 6 中,文件如下所示:

<ns2:recommendation>
    <id>4</id>
    <type>ARTICLE</type>
    <name>Application-4</name>
    <description>Great app called Application-4</description>
    <publisher>Medio</publisher>
    <category>Productivity</category>
    <updated>01062001</updated>
    <rating>4.0</rating>
    <price>.99</price>
    <currency>USD</currency>
    <iconURI>http://medio.com</iconURI>
    <downloadURI>http://medio.com</downloadURI>
    <ns2:extensions>
        <ns2:extension ns2:key="Sample">
            <ns2:value>Extension</ns2:value>
        </ns2:extension>
    </ns2:extensions>
    <ratingCount>35213614</ratingCount>
    <downloadCount>12435854</downloadCount>
</ns2:recommendation>

在 java 7 中,它是:

<ma:recommendation>
    <id>3</id>
    <type>ARTICLE</type>
    <name>Application-3</name>
    <description>Great app called Application-3</description>
    <publisher>Medio</publisher>
    <category>Productivity</category>
    <updated>01062001</updated>
    <rating>4.0</rating>
    <price>.99</price>
    <currency>USD</currency>
    <iconURI>http://medio.com</iconURI>
    <downloadURI>http://medio.com</downloadURI>
    <_extensions>
        <entry>
            <key>Sample</key>
            <value>Extension</value>
        </entry>
    </_extensions>
    <ratingCount>35213614</ratingCount>
    <downloadCount>12435854</downloadCount>
</ma:recommendation>

不同之处在于扩展标记,它映射到不会更改的 java 代码。有人知道如何解决吗?

扩展的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;
    }
}
4

1 回答 1

0

利用

public final class ExtensionMap {  
   private Map<String, String> holder = new HashMap<String, String>();  
   public String put(String key, String value)
   {
      return holder.put(key, value); 
   }   
   // ...

而不是扩展HashMap.

Java 6 使用

public List<ExtensionMapElement> getEntries() 

获取条目,但 Java 7 忽略此方法,因为您的类扩展HashMap

于 2013-04-17T08:36:55.740 回答