java - 具有继承的 Jaxb POJO 生成

标签 java jaxb pojo xml-binding

我想使用具有不同层次结构的 xml 绑定(bind)生成 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 中,我只放了 2 个数字,但我可以放更多。我希望能够在 xjb 中指定所有 complexType 都应该从 Figure 扩展,但只有少数应该从 ThreeSideFigure 扩展。

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

最佳答案

我不认为 Metro JAXB RI扩展程序会让你做到这一点。

来自2.2.7 documentation :

3.1.3. Extending a Common Super Class

The <xjc:superClass> customization allows you to specify the fully qualified name of the Java class that is to be used as the super class of all the generated implementation classes. The <xjc:superClass> customization can only occur within your <jaxb:globalBindings> customization on the <xs:schema> element

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

关于java - 具有继承的 Jaxb POJO 生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22067129/

相关文章:

java - 数据库或 CSV 到 java 对象

java - 如何在 Java 中使用两个参数创建 ObservableBooleanValue?

java - 无法构建 EntityManagerFactory + 未找到 JDBC 驱动程序类

java - 如何从字符串中获取浮点值

java - 我可以对不同的 Tomcat 实例进行 URL 重写吗?

java - jaxb.in​​dex 和嵌套类(和 OSGi)

java - JAXB 每个应用程序一个 Marshaller 实例和 Unmarshaller 实例

java - xjc 使用绑定(bind)时无法生成类

java - 从 POJO 调用基于 Spring 的 Web 应用程序类

java - 如何使用带有 JBOSS 工具的 Hibernate 在数据库中添加新记录?