[英]Jaxb POJOs generation with Inheritance
我想使用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生成的Triangle POJO應該從ThreeSideFigure
而不是Figure
擴展。
在這個特定的xsd我只放了兩個數字,但我可以有更多。 我希望能夠在xjb中指定所有complexType應該從圖中擴展,但只有少數幾個應該從ThreeSideFigure擴展。
你知道xjb應該是什么樣的嗎?
我認為Metro JAXB RI擴展不會讓你這樣做。
從2.2.7文檔 :
3.1.3。 擴展普通超級課程
<xjc:superClass>
自定義允許您指定要用作所有生成的實現類的超類的Java類的完全限定名稱。<xjc:superClass>
自定義只能在<xs:schema>
元素的<jaxb:globalBindings>
自定義中進行
那就是說, XJC超級接口和超類的答案僅適用於所有類? 建議您可以使用第三方擴展程序執行此操作。 文檔中有一些關於插件的細節 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.