【发布时间】:2015-03-07 11:27:22
【问题描述】:
我正在使用 JAXB 2.0 将 XML 字符串映射到 POJO,反之亦然,我希望能够确定 XML 中应该映射到带注释的 POJO 类中的元素。
假设我有以下 XML:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="1">
<firstName>Peter</firstName>
<lastName>Parker</lastName>
<socialSecurityNumber>112233445566</socialSecurityNumber>
</customer>
我想将它映射到一个省略 socialSecurityNumber 元素的类:
@XmlRootElement
public class Customer {
private Integer id;
private String firstName;
private String lastName;
public Customer() {
}
public Customer(Integer id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
@XmlAttribute
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@XmlElement(nillable = true)
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@XmlElement(nillable = true)
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
如果我尝试对此进行解组,则会收到错误消息:
意外元素(uri:“”,本地:“socialSecurityNumber”)。预期元素是
是否可以忽略我的 POJO 类中不存在的 XML 中的元素/属性?
【问题讨论】: