我試圖將一個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());
}
}
}
解組不工作_幾乎沒有任何信息。怎麼了? – Keppil
@Keppil:解組列表條目不起作用。散列圖總是空的。 – Siegi82AT