我有这个对象:
ClassRoom.java
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class ClassRoom {
@XmlElementWrapper(required = true)
private List<Person> students;
@XmlElement
private Person teacher;
@XmlElement(nillable = true)
private String name;
public List<Person> getStudents() {
return students;
}
public void setStudents(List<Person> students) {
this.students = students;
}
public Person getTeacher() {
return teacher;
}
public void setTeacher(Person teacher) {
this.teacher = teacher;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
使用此子对象类:
Person.java
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Person {
@XmlElement(nillable = true)
private String name;
private String lastname;
private BigDecimal moneySaved;
private List<String> certifications;
private String ubication;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public BigDecimal getMoneySaved() {
return moneySaved;
}
public void setMoneySaved(BigDecimal moneySaved) {
this.moneySaved = moneySaved;
}
public List<String> getCertifications() {
return certifications;
}
public void setCertifications(List<String> certifications) {
this.certifications = certifications;
}
public String getUbication() {
return ubication;
}
public void setUbication(String ubication) {
this.ubication = ubication;
}
}
我必须编组此结构,但是在某些情况下,ClassRoom实例的每个属性都可能为null。在这种情况下,XML输出必须如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<classRoom xmlns="emp">
<name></name>
<teacher>
<name></name>
<lastname></lastname>
<moneySaved></moneySaved>
<certifications></certifications>
<ubication></ubication>
</teacher>
<students></students>
</classRoom>
但是当我在这些对象的字段中使用nillable = true属性时,为了在null时显示空标签,XML看起来像这样:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<classRoom xmlns="emp">
<name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<students xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<teacher xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</classRoom>
我有以下问题(将对象编组为XML时):
是否可以从空表示形式标记中删除xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"
部分?
当根对象为null时,是否有任何选项或实现来编组空对象(及其嵌套对象)?