使用JAXB,我无法弄清楚如何编组Properties
。特别是,我试图整理SimpleObjectProperty
,但我认为它也不能与其他房产合作。在其他一些课程中,我能够管理getter / setter,但在HashMap
的{{1}}
这是我的代码段:
SimpleObjectProperties
这是输出XML片段:
..
@XmlRootElement(name = "derby")
@XmlAccessorType(XmlAccessType.FIELD)
public class Derby extends Observable {
@XmlElement(name = "cars")
private final ArrayList<Car> cars;
@XmlTransient
private final ObservableList<Car> carsObservable;
@XmlElement(name = "schedule")
private final SimpleObjectProperty<Schedule> scheduleProperty;
@XmlElement(name = "settings")
private final HashMap<String, SimpleObjectProperty> settings;//This is not marshalling correctly
..
我正在创建变量,而不是让它们未初始化。我还测试了在编组之前设置是否实际具有值,并且确实如此。
我使用...
<settings>
<entry>
<key>track.lanecount</key>
<value/> //No value???
</entry>
<entry>
<key>general.cardisplay</key>
<value/>
</entry>
<entry>
<key>track.orientation</key>
<value/>
</entry>
<entry>
<key>general.derbytitle</key>
<value/>
</entry>
</settings>
...
而不仅仅是我想要存储的真实对象的原因是因为我想要将侦听器添加到我的设置中,所以当单个设置发生更改时,所有依赖它的东西都会自动更新。
答案 0 :(得分:3)
我发现你正在使用 @XmlAccessorType(XmlAccessType.FIELD),这意味着类字段用于编组/解组值......
您可以将其更改为 @XmlAccessorType(XmlAccessType.PROPERTY),然后将所有 @XmlElement 放到属性的getter中。
请参阅文档了解更多信息:XmlAccessType
你的代码啜饮然后看起来像这样:
..
@XmlRootElement(name = "derby")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Derby extends Observable {
private final ArrayList<Car> cars;
private final ObservableList<Car> carsObservable;
private final SimpleObjectProperty<Schedule> scheduleProperty;
private final HashMap<String, SimpleObjectProperty> settings;//This is not marshalling correctly
@XmlElement(name = "cars")
public List<Car> getCars() {
return cars;
}
public void setCars(final ArrayList<Car> cars) {
this.cars.clear();
this.cars.addAll(cars);
}
@XmlElement(name = "schedule")
public Schedule getSchedule() {
return this.scheduleProperty.get();
}
public void setSchedule(final Schedule schedule) {
this.scheduleProperty.set();
}
@XmlElement(name = "settings")
public Map<String, Object> getSettings() {
// do your mapping logic here...
// convert your HashMap<String, SimpleObjectProperty> to Map<String, Object>
}
public void setSettings(final Map<String, Object> settings) {
// do your mapping logic here...
// convert your Map<String, Object> to HashMap<String, SimpleObjectProperty>
}
@XmlTransient
public ObservableList<Car> getCarsObservable() {
return carsObservable;
}
..