JAXB - 防止nillable属性xmlns:xsi =" http://www.w3.org/2001/XMLSchema-instance"的xsi:无="真"

时间:2014-04-15 13:53:20

标签: java xml jaxb

这是我的问题:我需要by对象的name标签出现在XML中,但没有nillable属性:简而言之,如<name/>。这是对象的代码。 如果name为null,我会得到标记<name xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>,但附加属性可能会给客户端带来问题。

正如我所读到的,从语义上讲,以这种方式表示空值是有意义的(使用@XmlElement(nillable=true))。

    package com.mns.mnsutilities.jaxb.model;

    import java.util.ArrayList;
    import java.util.List;

    import javax.xml.bind.annotation.XmlAttribute;
    import javax.xml.bind.annotation.XmlElement;
    import javax.xml.bind.annotation.XmlElementWrapper;
    import javax.xml.bind.annotation.XmlRootElement;
    import javax.xml.bind.annotation.XmlTransient;
    import javax.xml.bind.annotation.XmlType;


    @XmlRootElement(name="Emp_MNS")
    @XmlType(propOrder= {"name", "age", "role", "gender", "addressesList"})
    public class Employee {
        private int id;
        private String gender;
        private int age;
        private String name;
        private String role;
        private String password;
        private List<Address> addressesList;

        public Employee() {}

        public Employee(int id, String gender, int age, String name, String role,
                String password) {
            super();
            this.id = id;
            this.gender = gender;
            this.age = age;
            this.name = name;
            this.role = role;
            this.password = password;
        }

        public Employee(int id, String gender, int age, String name, String role,
                String password, List<Address> addressesList) {
            super();
            this.id = id;
            this.gender = gender;
            this.age = age;
            this.name = name;
            this.role = role;
            this.password = password;
            this.addressesList = addressesList;
        }

        @XmlAttribute
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        @XmlElement(name = "gen", nillable=true)
        public String getGender() {
            return gender;
        }
        public void setGender(String gender) {
            this.gender = gender;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        @XmlElement(nillable=true)
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getRole() {
            return role;
        }
        public void setRole(String role) {
            this.role = role;
        }
        @XmlTransient
        public String getPassword() {
            return password;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        @XmlElementWrapper( name="addresses" )
        @XmlElement(name = "address")
        public List<Address> getAddressesList() {
            if(addressesList == null){
                addressesList = new ArrayList<>();
            }
            return addressesList;
        }

        public void setAddressesList(List<Address> addressesList) {
            this.addressesList = addressesList;
        }

        @Override
        public String toString() {
            return "Employee [id=" + id + ", gender=" + gender + ", age=" + age
                    + ", name=" + name + ", role=" + role + ", password="
                    + password + ", addressesList=" + addressesList + "]";
        }
    }

2 个答案:

答案 0 :(得分:2)

扩展Ian Roberts的评论,您可以利用字段访问权限,并让该属性将字段值""视为空。

@XmlRootElement(name="Emp_MNS")
@XmlAccessorType(XmlAccessType.FIELD)
public class Employee {

    private String name = "";

    public String getName() {
        if(name.length() == 0) {
            return null;
        }
        return name;
    }

    public void setName(String name) {
        if(null == name) {
            this.name = "";
        } else {
            this.name = name;
        }
    }

答案 1 :(得分:0)

我遇到了同样的问题,但是我的领域是枚举,不能是空字符串;所以我只用

myXml.replaceAll("xsi:nil=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"","")

在另一个解决方案中,我向枚举添加了空字符串值,例如

@XmlEnum
@Getter
public enum MyEnum {


    @XmlEnumValue("1") Apple("1"),
    @XmlEnumValue("2") Banner("2"),
    @XmlEnumValue("") NULL("3");

    MyEnum(String value, String description) {
        this.value = value;
        this.description = description;
    }

    private String value;

    private String description;
}

以便我可以将MyEnum.NULL设置为已归档;