JAXB - XML Schema Types, Defining Subtypes
Although object orientation isn't a key feature of XML or the XML Schema language, it's still possible to apply the fundamental OO paradigm when designing a schema: inheritance. This is based on the schema element xsd:extension
which lets you add both child elements and attributes to some elsewhere defined type acting as the base type. The example given below presents the components for defining a simple menu (this time it's for a graphical user interface) where menu entries may come in several flavours: simple items, check boxes, radio buttons and sub-menus.
<xsd:complexType name="EntryType"> <xsd:attribute name="Text" type="xsd:string"/> </xsd:complexType> <xsd:complexType name="ItemType"> <xsd:complexContent> <xsd:extension base="EntryType"> <xsd:sequence> <xsd:element name="Command" type="xsd:string"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:complexType name="CheckBoxType"> <xsd:complexContent> <xsd:extension base="ItemType"> <xsd:attribute name="State" type="xsd:boolean"/> </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:complexType name="RadioButtonType"> <xsd:complexContent> <xsd:extension base="ItemType"> <xsd:attribute name="Group" type="xsd:string"/> <xsd:attribute name="State" type="xsd:boolean"/> <xsd:attribute name="Value" type="xsd:string"/> </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:complexType name="MenuType"> <xsd:complexContent> <xsd:extension base="EntryType"> <xsd:choice maxOccurs="unbounded"> <xsd:element name="Item" type="ItemType"/> <xsd:element name="CheckBox" type="CheckBoxType"/> <xsd:element name="RadioButton" type="RadioButtonType"/> <xsd:element name="Menu" type="MenuType"/> </xsd:choice> </xsd:extension> </xsd:complexContent> </xsd:complexType>
The base class EntryType
is extended in several ways:
ItemType
adds a command definition to the base type.- CheckBoxType extends ItemType, inheriting the command and adding an attribute for the initial state of the check box.
- RadioButtonType is another extension of ItemType, again adding some attributes. Group is the button group's identification, and Value defines the string to be used for indicating the selection.
- MenuType reflects the recursive structure of menus by being both another subclass of ItemType (so that it may represent cascades) as well as a container for all kinds of menu entries, including itself.
Before we look at the generated Java code, we should note that the definition of MenuType
isn't quite what an OO aficionado would expect. After all the pains taken to establish this little class hierarchy, one still must explicitly put all the subclasses into the choice list. Using just the supertype EntryType
in an xsd:sequence
would result in very dull menus.
The JAXB compiler, however, rewards you with a set of class definitions that uses extends
wherever we have xsd:extension
in the schema. A look at the (much abbreviated) code shows the expected inheritance structure.
public class EntryType { protected String text; // ...(getText, setText) } public class ItemType extends EntryType { protected String command; // ...(getCommand, setCommand) } public class CheckBoxType extends ItemType { protected Boolean state; // ...(isState, setState) } public class RadioButtonType extends ItemType { protected String group; protected Boolean state; protected String value; // ...(getters and setters) }
Finally there is MenuType
, which contains a java.util.List<EntryType>
. JAXB has briefly reflected upon the element types bunched into the choice and has, literally, reverse engineered the common superclass. A reminder that the list is a mixture is embedded in the name of the getter which is made up from the first three tags.
public class MenuType extends EntryType { protected List<EntryType> itemOrCheckBoxOrRadioButton; public List<EntryType> getItemOrCheckBoxOrRadioButton() { if (itemOrCheckBoxOrRadioButton == null) { itemOrCheckBoxOrRadioButton = new ArrayList<EntryType>(); } return this.itemOrCheckBoxOrRadioButton; } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 本地部署 DeepSeek:小白也能轻松搞定!
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 从 Windows Forms 到微服务的经验教训
· 李飞飞的50美金比肩DeepSeek把CEO忽悠瘸了,倒霉的却是程序员
· 超详细,DeepSeek 接入PyCharm实现AI编程!(支持本地部署DeepSeek及官方Dee
2015-05-18 Spring - IoC(12): 属性占位符
2015-05-18 Tomcat - 设置 HTTP 基本认证
2015-05-18 HTTP - 基本认证
2015-05-18 HTTP - 首部