简体   繁体   English

JAXB编组和多态

[英]JAXB Marshalling and Polymorphism

I have a hierarchy of JAXB-generated classes. 我有一个JAXB生成的类的层次结构。 I would like to marshal a child class as a base class element (but with all the child class attributes), using xsi:type to indicate the concrete type. 我想将子类编组为基类元素(但具有所有子类属性),使用xsi:type来指示具体类型。

For example, given an Animal and a Bird subclass: 例如,给定Animal和Bird子类:

<xs:complexType name="animal" abstract="true">
    <xs:sequence>
        <xs:element name="name" type="xs:string"/>
    </xs:sequence>
</xs:complexType>

<xs:complexType name="bird">
    <xs:complexContent>
        <xs:extension base="animal">
            <xs:sequence>
                <xs:element name="maxAltitude" type="xs:int"/>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

<xs:element name="Animal" type="animal"/>
<xs:element name="Bird" type="bird"/>

No matter how I marshal a Bird, for example: 不管我如何编组一只鸟,例如:

Bird sparrow = new Bird();
sparrow.setName("Sparrow");
sparrow.setMaxAltitude(1000);

JAXBContext context = JAXBContext.newInstance(Animal.class, Bird.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(sparrow, System.out);

The result is always a Bird element: 结果始终是Bird元素:

<Bird xmlns="http://mycompany.com/animals">
    <name>Sparrow</name>
    <maxAltitude>1000</maxAltitude>
</Bird>

However what I want is this (all attributes of subclass, xsi type, base class element name): 但是我想要的是这个(子类的所有属性,xsi类型,基类元素名称):

<Animal xmlns="http://mycompany.com/animals"
        xsi:type="bird"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <name>Sparrow</name>
    <maxAltitude>1000</maxAltitude>
</Animal>

What's odd is that if I create a wrapper element: 奇怪的是,如果我创建一个包装元素:

<xs:complexType name="animalWrapper">
    <xs:sequence>
        <xs:element name="Animal" type="animal"/>
    </xs:sequence>
</xs:complexType>

<xs:element name="AnimalWrapper" type="animalWrapper"/>

and marshal it, it uses the base class type: 并编组它,它使用基类类型:

<AnimalWrapper xmlns="http://mycompany.com/animals"
    <Animal xsi:type="bird"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <name>Sparrow</name>
        <maxAltitude>1000</maxAltitude>
    </Animal>
</AnimalWrapper>

If I manually build my desired XML document, JAXB has no problem unmarshalling it. 如果我手动构建我想要的XML文档,JAXB解组它没有问题。 How can I author my XML schema and/or configure JAXB to allow my desired marshalling behavior? 如何创建XML模式和/或配置JAXB以允许我所需的编组行为?

Thanks. 谢谢。

You could do the following: 您可以执行以下操作:

QName qName = jc.createJAXBIntrospector().getElementName(new Animal());
JAXBElement<Animal> jaxbElement = new JAXBElement<Animal>(qName, Animal.class, new Bird());
marshaller.marshal(jaxbElement, System.out);

Check out: 查看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM