JAXB Marshalling - extending an existing class

2019-03-31 05:52发布

问题:

I am in need of creating a series of Java objects via XML using JAXB that all extend a common base class that is already created (not using JAXB). For example, let's say I have following JAXB classes that I am trying to generate:

Penguin.xml  -> Penguin.java
Robin.xml -> Robin.java
Cardinal.xml -> Cardinal.java

I already have an existing base class called Bird.java that I wish the three classes above to extend.

What is the best way to do this?

Thanks for your help!

回答1:

That is very simple: you need to to create a JAXB binding file with following contents:

<jaxb:bindings version="1.0"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
>

    <jaxb:globalBindings>
        <!-- All beans should extend this base class: -->
        <xjc:superClass name="org.mycompany.common.Bird" />
    </jaxb:globalBindings>

</jaxb:bindings>

More information on this option (and other sweet things) you can find here.