序列化为XML时忽略父类

时间:2010-09-16 15:46:00

标签: java data-binding xml-serialization jaxb

当子类的List上有@XmlElement时,是否有JAXB注释忽略父类?

只是为了澄清 - 我想知道是否有一种更好的方法,而不是将所有父类getter / setter标记为瞬态,然后必须返回子类并添加getter / setter并将其注释为XmlElements as井

一个例子:

public class GenericHelper {
    String name="";
    String dates="";
    String roleName="";
    String loe="";
    @XmlTransient
    public String getName() {return name;}
public void setName(String name) {this.name = name;}
@XmlTransient
public String getDates() {return dates;}
public void setDates(String dates) {this.dates = dates;}
@XmlTransient
public String getRoleName() {return roleName;}
public void setRoleName(String roleName) {this.roleName = roleName;}
@XmlTransient
public String getLOE() {return loe;}
public void setLOE(String loe) {
    this.loe = loe.replace("%", "").trim();
}
}

public class SpecificHelper extends GenericHelper {
List<ProjectHelper> projects;
public SpecificHelper (){
    projects=new ArrayList<ProjectHelper>();
}
@XmlElement(name = "project")
@XmlElementWrapper (name = "projectlist")
public List<ProjectHelper> getProjects() {return projects;}
public void setProjects(List<ProjectHelper> projects) {this.projects = projects;}
@XmlElement
public String getName(){
    return super.getName();
}

@Override
public String toString(){
    String ret="SpecificHelper [";
    ret+="name:"+name+";";
    ret+="dates:"+dates+";";
    ret+="roleName:"+roleName+";";
    ret+="loe:"+loe+";";
    ret+="\n\tprojects:"+projects+";";
    return ret+"]";
}
}

所以在这个例子中,如果我取出GenericHelper中的XmlTransient注释,任何扩展它的类,如果我有一个方法getSpecificHelper()返回所有雇主的列表,并用XmlElement注释它,ALL这些项目将返回一个名称,LOE,RoleName等。我正在寻找一个类注释继续GenericHelper所以我可以避免必须单独使用所有@XmlTransients,并只使用我放入的XmlElement符号SpecificHelper

1 个答案:

答案 0 :(得分:4)

怎么样?

父类

我们将使用XmlAccessType.NONE告诉JAXB只映射明确注释的字段/属性。

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;

@XmlAccessorType(XmlAccessType.NONE)
public class Parent {

    private String parentProperty1;
    private String parentProperty2;

    public String getParentProperty1() {
        return parentProperty1;
    }

    public void setParentProperty1(String parentProperty1) {
        this.parentProperty1 = parentProperty1;
    }

    public String getParentProperty2() {
        return parentProperty2;
    }

    public void setParentProperty2(String parentProperty2) {
        this.parentProperty2 = parentProperty2;
    }

}

儿童班

我们将在子节点上使用XmlAccessType.PROPERTY。我们想要包含的父类中的任何属性都需要被覆盖并显式注释。在这个例子中,我们将从Parent类引入parentProperty2。您只需要从父类重写getter。

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Child extends Parent {

    private String childProperty;

    @Override
    @XmlElement
    public String getParentProperty2() {
        return super.getParentProperty2();
    }

    public String getChildProperty() {
        return childProperty;
    }

    public void setChildProperty(String childProperty) {
        this.childProperty = childProperty;
    }

}

演示班

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Child.class);

        Child child = new Child();
        child.setParentProperty1("parentProperty1");
        child.setParentProperty2("parentProperty2");
        child.setChildProperty("childProperty");

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(child, System.out);
    }
}

<强>输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<child>
    <childProperty>childProperty</childProperty>
    <parentProperty2>parentProperty2</parentProperty2>
</child>