4

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!

risingTide
  • 1,754
  • 7
  • 31
  • 60

1 Answers1

7

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.

dma_k
  • 10,431
  • 16
  • 76
  • 128
  • 1
    Is there a way to specify witch classes extend Bird class. What if I want only Robin and Cardinal to extend Bird class? – Chobicus Aug 08 '12 at 08:53
  • 1
    +1 also if you get an error: "vendor extension bindings (jaxb:extensionBindingPrefixes) are not allowed in the strict mode." your should use -extension, if in maven pom use: -extension...in maven-jaxb2-plugin – user1697575 May 30 '13 at 15:03
  • @Chobicus: The answer to your question is given [here](http://stackoverflow.com/a/4984018/267197) in particular you need to activate [Inheritance plugin](http://confluence.highsource.org/display/J2B/Inheritance+plugin). – dma_k May 31 '13 at 11:19