JAXB Marshalling - extending an existing class

这一生的挚爱 提交于 2019-12-05 01:59:09

问题


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.



来源:https://stackoverflow.com/questions/9364178/jaxb-marshalling-extending-an-existing-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!