使用jaxb进行marshelling时使用派生类 [英] Using derived classes when marshelling with jaxb

查看:77
本文介绍了使用jaxb进行marshelling时使用派生类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有公共基类的对象列表,我试图使用jaxb将其序列化为XML。我希望在编组时使用派生类的注释,但是我遇到了麻烦。

I have a list of objects with a common base class that I am trying to serialise as XML using jaxb. I would like the annotations of the derived classes to be used when marshalling, but I'm having trouble getting there.

import java.util.Arrays;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlRootElement;


public class Runner {
    @XmlRootElement(name="base")
    public static abstract class Base {
        public int baseValue;

        public Base() {
            this.baseValue = 0;
        }
    }

    @XmlRootElement(name="derived")
    public static class Derived extends Base {
        public int derivedValue;

        public Derived() {
            super();
            this.derivedValue = 1;
        }
    }

    @XmlRootElement(name="derived2")
    public static class Derived2 extends Base {
        public int derivedValue;

        public Derived() {
            super();
            this.derivedValue = 1;
        }
    }

    @XmlRootElement(name="base_list")
    public static class BaseList {
        public List<Base> baseList;
    }

    public static void main(String[] args) throws JAXBException {
        BaseList baseList = new BaseList();
        baseList.baseList = Arrays.asList((Base) new Derived(), (Base) new Derived());

        JAXBContext jaxbContext = JAXBContext.newInstance(BaseList.class);

        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        marshaller.marshal(baseList, System.out);

    }
}

我想:

<base_list>
    <derived>
        <baseValue>0</baseValue>
        <derivedValue>1</derivedValue>
    </derived>
    <derived2>
        <baseValue>0</baseValue>
        <derivedValue>1</derivedValue>
    </derived2>
</base_list>

但是,上面的代码是:

<base_list>
    <baseList>
        <baseValue>0</baseValue>
    </baseList>
    <baseList>
        <baseValue>0</baseValue>
    </baseList>
</base_list>

有没有办法强迫它使用派生类?在实际情况中,我事先并不知道可能来自Base的类。

Is there any way to force it to use the derived class? In the real situation I don't know ahead of time the classes that may derive from Base.

请注意,我只需要编组,而不是解组数据。

Note that I only need to marshal, not unmarshal the data.

推荐答案

您可以使用 @XmlElementRef 批注来处理此用例。 @XmlElementRef 对应于XML模式中替换组的概念。

You can use the @XmlElementRef annotation to handle this use case. @XmlElementRef corresponds to the concept of substitution groups in XML schema.

例如:

  • http://bdoughan.blogspot.com/2010/11/jaxb-and-inheritance-using-substitution.html

这篇关于使用jaxb进行marshelling时使用派生类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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