繁体   English   中英

使用JAXB解组具有属性的元素

[英]Unmarshalling an element with attributes using JAXB

我可以成功地将对象编组为XML,反之亦然,除了具有属性的2个元素外。 任何命中我所缺少的。 我两个属性都为空(角色)

解组码

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

您的Role setter方法不是真正的setter方法,并且Role字段被标记为final ,从而阻止您为类提供该字段的true setter方法。

我建议您摆脱final修饰符,并为User类赋予Role字段真正的setter方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM