具有继承性的Jaxb POJO生成

时间:2014-02-27 11:28:39

标签: java jaxb pojo xml-binding

我想使用xml-binding生成POJO,它具有我现在拥有的不同层次结构。 现在我有一个像这样的xsd:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://manueldoncel.com" xmlns:tns="http://manueldoncel.com"  elementFormDefault="qualified">
    <!-- Base element that any element uses / extends  -->
    <complexType name="baseElement" abstract="true">
        <sequence>
            <element name="attributes" type="anyType"  minOccurs="0" />
        </sequence>
        <attribute name="id" type="string" />
    </complexType>

    <complexType name="Square">
        <complexContent><extension base="tns:baseElement" /></complexContent>
    </complexType>

    <complexType name="Triangle">
        <complexContent><extension base="tns:baseElement" /></complexContent>
    </complexType>

</schema>

像这样的xjb;

<?xml version="1.0"?>
<jxb:bindings version="1.0" xmlns:jxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" jxb:extensionBindingPrefixes="xjc" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <jxb:bindings schemaLocation="figures.xsd" node="/xs:schema">
    <jxb:globalBindings>
      <xjc:simple />
      <xjc:superClass name="org.manuel.metadata.Figure"/>     
    </jxb:globalBindings>
  </jxb:bindings>
</jxb:bindings>

但是,我希望通过这种方法获得更好的层次结构

public abstract class Figure {
    // some methods for all the figures
}

public abstract class ThreeSideFigure extends Figure {
.... // some methods for only the Three side Figures
}

因此,从XSD生成的三角形POJO应该从ThreeSideFigure而不是Figure延伸。

在这个特定的xsd我只放了两个数字,但我可以有更多。我希望能够在xjb中指定所有complexType应该从图中扩展,但只有少数几个应该从ThreeSideFigure扩展。

你知道xjb应该是什么样的吗?

1 个答案:

答案 0 :(得分:2)

我认为Metro JAXB RI扩展程序不会让您这样做。

来自2.2.7 documentation

  

<强> 3.1.3。扩展普通超级课程

     

<xjc:superClass>自定义允许您完全指定   要用作超类的Java类的限定名称   所有生成的实现类。 <xjc:superClass>   自定义只能在<jaxb:globalBindings>内进行   <xs:schema>元素

上的自定义

也就是说,XJC superinterface and superclass only for all classes?的答案表明您可以通过第三方扩展来实现。有一些details about plugins in the documentation