2014-02-21 49 views
1

我試圖將一個HashMap編組並解組爲一個列表。編組可在兩個Java版本(6u45和7u21)中使用,但使用Java 6u45解組列表條目將提供一個空的myModels-HashMap。在Java 7u21中,myModels-HashMap被填充。我試過用Java 6u45(將jaxb-api.jar複製到lib/endorsed目錄)和其他jaxb- [core | impl | jxc | xjc]使用JAXB 2.2.7到我的應用程序庫中,將它們添加到classpath),但我得到了相同的結果。JAXB解組列表Java 6不工作,Java 7工作

感謝您的回答。

編組XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<myStore> 
    <myModels> 
     <value>My Model 2</value> 
     <myEnum>VALUE2</myEnum> 
    </myModels> 
    <myModels> 
     <value>My Model 1</value> 
     <myEnum>VALUE1</myEnum> 
    </myModels> 
    <name>My Store</name> 
</myStore> 

爲MyModel:

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlRootElement; 

@XmlRootElement 
@XmlAccessorType(XmlAccessType.FIELD) 
public class MyModel { 
public enum MyEnum { 
    VALUE1, VALUE2 
} 

private String value; 
private MyEnum myEnum; 

public MyModel() { 

} 

public MyModel(String value, MyEnum myEnum) { 
    this.setValue(value); 
    this.setMyEnum(myEnum); 
} 

public String toString() { 
    return getValue() + ", " + getMyEnum(); 
} 

public String getValue() { 
    return value; 
} 

public void setValue(String value) { 
    this.value = value; 
} 

public MyEnum getMyEnum() { 
    return myEnum; 
} 

public void setMyEnum(MyEnum myEnum) { 
    this.myEnum = myEnum; 
} 

}

的MyStore:

import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlRootElement; 

@XmlRootElement 
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER) 
public class MyStore { 
    private String name; 
    private Map<String, MyModel> myModels = new HashMap<String, MyModel>(); 

    public MyStore() { 

    } 

    public MyStore(String name) { 
     this.setName(name); 
    } 

    public void addMyModel(MyModel myModel) { 
     myModels.put(myModel.getValue(), myModel); 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public List<MyModel> getMyModels() { 
     return new ArrayList<MyModel>(myModels.values()); 
    } 

    public void setMyModels(List<MyModel> myModels) { 
     for (MyModel myModel: myModels) { 
      this.myModels.put(myModel.getValue(), myModel); 
     } 
    } 
} 

測試類編組/解組

import java.io.File; 

import javax.xml.bind.JAXB; 
import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBException; 
import javax.xml.bind.Unmarshaller; 

import MyModel.MyEnum; 

public class MyStoreTest { 
    public static void main(String[] args) throws JAXBException { 
     MyStore myStore = new MyStore("My Store"); 
     myStore.addMyModel(new MyModel("My Model 1", MyEnum.VALUE1)); 
     myStore.addMyModel(new MyModel("My Model 2", MyEnum.VALUE2)); 

     File fileMyStore = new File("MyStore.xml"); 

     JAXB.marshal(myStore, fileMyStore); 

     JAXBContext context = JAXBContext.newInstance(MyStore.class, 
       MyModel.class); 
     Unmarshaller m = context.createUnmarshaller(); 
     MyStore myStoreFromXml = (MyStore) m.unmarshal(fileMyStore); 
     System.out.println(myStoreFromXml.getName()); 
     for (MyModel myModel : myStoreFromXml.getMyModels()) { 
      System.out.println(myModel.toString()); 
     } 
    } 
} 
+0

解組不工作_幾乎沒有任何信息。怎麼了? – Keppil

+0

@Keppil:解組列表條目不起作用。散列圖總是空的。 – Siegi82AT

回答

2

當解組List財產JAXB將檢查的get方法,看是否有List已經被初始化。如果有人在那裏,它將被用來添加值。這使您可以選擇底層的List實現。你的get方法總是返回一個ArrayList的實例。由於它沒有被任何東西引用,所以解集到的數據會丟失。

如果您的Map爲空,您可以讓您的財產返回null而不是空的ArrayList以使您的用例正常工作。

+0

感謝您的詳細解釋。如果我返回null問題是,在setMyModels myModels總是空的(大小爲0)。我應該使用自定義適配器來解決這個問題嗎? – Siegi82AT