【问题标题】:Jaxb POJOs generation with Inheritance带有继承的 Jaxb POJO 生成
【发布时间】:2014-02-27 11:28:39
【问题描述】:

我想使用我现在拥有的不同层次结构的 xml 绑定来生成 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 生成的 Triangle POJO 应该从 ThreeSideFigure 扩展,而不是从 Figure 扩展。

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

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

【问题讨论】:

    标签: java jaxb pojo xml-binding


    【解决方案1】:

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

    来自2.2.7 documentation

    3.1.3。扩展通用超类

    &lt;xjc:superClass&gt; 自定义允许您指定完整的 用作超类的 Java 类的限定名 所有生成的实现类。 &lt;xjc:superClass&gt; 定制只能在您的&lt;jaxb:globalBindings&gt; 中进行 &lt;xs:schema&gt; 元素上的自定义

    也就是说,XJC superinterface and superclass only for all classes? 的答案表明您可以使用 3rd 方扩展来做到这一点。还有一些details about plugins in the documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-04
      • 2012-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-01
      相关资源
      最近更新 更多