2013-04-17 79 views
0

我有一個jaxb代碼,它曾經在java 6中工作,但在更新到java 7之後,它生成的xml文件有所不同。jaxb差異bwteen java 7和java 6

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; 
    } 
} 
+0

PLS,添加類在使用該地圖 – Ilya

回答

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); 
    } 
    // ... 

,而不是extanding HashMap

Java 6中使用

public List<ExtensionMapElement> getEntries() 

得到的條目,但Java 7中忽略此方法becouse你類擴展HashMap