2014-06-24 43 views
1

我能夠將對象成功編組爲XML,反之亦然,除了2個具有屬性的元素。任何命中我失蹤。我得到空了每個屬性的(角色)使用JAXB解組具有屬性的元素

的unmarschel代碼

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

您用來映射「User」元素的任何類的源代碼是什麼? – jarnbjo

+0

我添加了缺失的類。 – iFadi

+0

你的角色setter方法不是真正的setter方法,你的Role字段被標記爲'final',從而阻止你爲這個字段賦予你的類一個真正的setter方法。爲什麼?爲什麼不擺脫最後一個修飾符,併爲User字段賦予User類真正的setter方法。 –

回答

1

您的角色setter方法是不正確的setter方法,你的角色字段標final,防止你給你的類此領域的真正setter方法。

我建議你擺脫最後的修飾符,併爲User類賦予User類真正的setter方法。

+0

我將方法改爲真正的setter方法,它確實有效!但有趣的只有一個角色,我回到了「全局角色」,但對於「本地角色」,我得到一個空值,我想知道這兩個角色的@XmlElement(name =「Role」)是否有負面影響 – iFadi

+0

做了一個測試,並更改了全局角色的@XmlElement(name =「Role1」)和本地角色的@XmlElement(name =「Role2」),它完美運行。有沒有辦法讓兩個角色的@XmlElement(name =「Role」)保持正常工作狀態...... – iFadi