当序列化为XML时,忽略父类 [英] Ignore a parent class when Serializing to XML

查看:121
本文介绍了当序列化为XML时,忽略父类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



只是为了澄清 - 我想知道,有没有一个JAXB注释来忽略一个父类,当你的子类列表中有一个@XmlElement如果有一个比标记所有父类getter / setter更好的方式作为暂时的,然后不得不返回到子类,并添加getter / setter并将它们注释为XmlElements



一个例子:

  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>项目;
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 + =\\\
\tprojects:+ projects +;;
return ret +];
}
}

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

解决方案

如何?



家长班



我们将使用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;
}

}

/ strong>



我们将在孩子上使用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.0encoding =UTF-8standalone =yes?> 
< child>
< childProperty> childProperty< / childProperty>
< parentProperty2> parentProperty2< / parentProperty2>
< / child>


Is there a JAXB annotation to ignore a parent class, when you have an @XmlElement on a List of the child classes?

Just to clarify - I was wondering if there was a better way than marking all of the parent classes getters/setters as transient, and then having to go back to the child classes and add getters/setters and annotating those as XmlElements as well

An Example:

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();
}
}

and

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+"]";
}
}

So in this example, if I take out the XmlTransient annotations in GenericHelper, any class that extends it, if I were to have a method getSpecificHelper() that returned a list of all employers, and annotate it with XmlElement, ALL of those items will return with a name, LOE, RoleName, etc. I am looking for a class annotation to go on GenericHelper so I can avoid having to use all of the @XmlTransients individually, and only use the XmlElement notations I have put in the SpecificHelper

解决方案

How about?

The Parent Class

We will use XmlAccessType.NONE to tell JAXB that only explicitly annotated fields/properties are mapped.

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

}

The Child Class

We will use XmlAccessType.PROPERTY on the child. Any properties from the parent class we want to include will need to be overridden and be explicitly annotated. In this example we will bring in parentProperty2 from the Parent class. You will only need to override the getter from the parent class.

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

}

Demo Class

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);
    }
}

Output

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

这篇关于当序列化为XML时,忽略父类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆