我能够成功地将对象编组为XML,反之亦然,除了2个具有属性的元素。我所缺少的任何一击。我为两个属性(角色)
获取nullunmarschel Code
JAXBContext context = JAXBContext
.newInstance(UserListWrapper.class);
Unmarshaller um = context.createUnmarshaller();
// Reading XML from the file and unmarshalling.
UserListWrapper wrapper = (UserListWrapper) um.unmarshal(file);
userData.clear();
userData.addAll(wrapper.getUsers());
这是XML文件
<Users>
<User Action="Insert" Id="test.user" Language="de">
<Birthday>2000-01-01</Birthday>
<City>ads</City>
<Firstname>test</Firstname>
<Gender>f</Gender>
<Role Action="Assign" Id="wqeqw" Type="Global">wqeqw</Role>
<Lastname>user</Lastname>
<Role Action="Assign" Id="qweqwe" Type="Local">qweqwe</Role>
<Login>sfrohwein</Login>
<Matriculation>ads</Matriculation>
<PostalCode>0</PostalCode>
<Street>asd</Street>
</User><Users>
班级角色
@XmlRootElement(name="Role")
public class Role {
private String id;
private String type;
private String action;
public Role() {
this(null,null);
}
public Role(String type) {
setType(type);
}
public Role(String id, String type) {
setId(id);
setType(type);
setAction("Assign");
}
/**
* @return the id
*/
@XmlAttribute(name="Id")
public String getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the type
*/
@XmlAttribute(name="Type")
public String getType() {
return type;
}
/**
* @param type the type to set
*/
public void setType(String type) {
this.type = type;
}
/**
* @return the action
*/
@XmlAttribute(name="Action")
public String getAction() {
return action;
}
/**
* @param action the action to set
*/
public void setAction(String action) {
this.action = action;
}
/**
* @return the value
*/
@XmlValue
public String getValue() {
return this.id;
}
班级用户
public class User {
private final Role globalRole;
private final Role localRole;
private final StringProperty login;
private final StringProperty firstName;
private final StringProperty lastName;
private final StringProperty matriculation;
private final StringProperty gender;
private final StringProperty street;
private final IntegerProperty postalCode;
private final StringProperty city;
private final ObjectProperty<LocalDate> birthday;
/**
* Default constructor.
*/
public User() {
this(null, null);
}
/**
* Constructor with some initial data.
*
* @param firstName
* @param lastName
*/
public User(String firstName, String lastName) {
this.firstName = new SimpleStringProperty(firstName);
this.lastName = new SimpleStringProperty(lastName);
// Some initial dummy data, just for convenient testing.
this.globalRole = new Role("Global");
this.localRole = new Role("Local");
this.gender = new SimpleStringProperty("f");
this.login = new SimpleStringProperty("");
this.matriculation = new SimpleStringProperty("");
this.street = new SimpleStringProperty("");
this.postalCode = new SimpleIntegerProperty();
this.city = new SimpleStringProperty("");
this.birthday = new SimpleObjectProperty<LocalDate>(LocalDate.of(2000, 1, 1));
}
@XmlElement(name = "Login")
public String getLogin() {
return login.get();
}
public void setLogin(String login) {
this.login.set(login);
}
public StringProperty LoginProperty() {
return login;
}
@XmlElement(name = "Matriculation")
public String getMatriculation() {
return matriculation.get();
}
public void setMatriculation(String matriculation) {
this.matriculation.set(matriculation);
}
public StringProperty MatriculationProperty() {
return matriculation;
}
@XmlElement(name = "Firstname")
public String getFirstName() {
return firstName.get();
}
public void setFirstName(String firstName) {
this.firstName.set(firstName);
}
public StringProperty firstNameProperty() {
return firstName;
}
@XmlElement(name = "Lastname")
public String getLastName() {
return lastName.get();
}
public void setLastName(String lastName) {
this.lastName.set(lastName);
}
public StringProperty lastNameProperty() {
return lastName;
}
@XmlElement(name = "Street")
public String getStreet() {
return street.get();
}
public void setStreet(String street) {
this.street.set(street);
}
public StringProperty streetProperty() {
return street;
}
@XmlElement(name = "PostalCode")
public int getPostalCode() {
return postalCode.get();
}
public void setPostalCode(int postalCode) {
this.postalCode.set(postalCode);
}
public IntegerProperty postalCodeProperty() {
return postalCode;
}
@XmlElement(name = "City")
public String getCity() {
return city.get();
}
public void setCity(String city) {
this.city.set(city);
}
public StringProperty cityProperty() {
return city;
}
@XmlElement(name = "Birthday")
@XmlJavaTypeAdapter(LocalDateAdapter.class)
public LocalDate getBirthday() {
return birthday.get();
}
public void setBirthday(LocalDate birthday) {
this.birthday.set(birthday);
}
public ObjectProperty<LocalDate> birthdayProperty() {
return birthday;
}
@XmlElement(name = "Gender")
public String getGender() {
return gender.get();
}
public void setGender(String gender) {
this.gender.set(gender);
}
public StringProperty GenderProperty() {
return gender;
}
@XmlElement(name = "Role")
public Role getGlobalRole() {
return globalRole;
}
public void setGlobalRole(String globalRole) {
this.globalRole.setId(globalRole);
}
@XmlElement(name = "Role")
public Role getLocalRole() {
return localRole;
}
public void setLocalRole(String localRole) {
this.localRole.setId(localRole);
}
类UserListWrapper
@XmlRootElement(name = "Users")
public class UserListWrapper {
private List<User> users;
@XmlElement(name = "User")
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
}
答案 0 :(得分:1)
您的角色设置器方法不是真正的setter方法,并且您的角色字段标记为final
,从而阻止您为此类提供真正的setter方法。
我建议您删除final修饰符,并为Role字段提供User类的真实setter方法。