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;
}
}

JAXB - XML Schema Types, Defining Subtypes的更多相关文章

  1. JAXB - XML Schema Types, Defining an Enumeration

    If you want a data type that enumerates discrete values you should use a restriction of the schema t ...

  2. JAXB - XML Schema Types, Defining Types for XML Elements With Content

    Content: A Value The content of an XML element may be some value, or one or more subordinate element ...

  3. JAXB - XML Schema Types, Defining Types for XML Elements Without Content

    Types for XML elements are constructed using xsd:complexType, even if they do not have content. The ...

  4. JAXB - XML Schema Types, Date and Time

    JAXB binds all three of the schema types xsd:date, xsd:time and xsd:dateTime to XMLGregorianCalendar ...

  5. JAXB - XML Schema Types, Binary Data

    Data that has no "natural" representation with printable characters must, for inclusion in ...

  6. XML Schema and XMLspy notes

    Introduction An xml documents consists of elements, attributes and text. There are two structures in ...

  7. [BTS] System.Xml.Schema.XmlSchemaException: The complexType has already been declared when generate IDoc schema.

    I use wcf-sap adapter for generate the schema of IDoc that named "YHREMPMASTER". but throw ...

  8. [CXF REST标准实战系列] 一、JAXB xml与javaBean的转换

    Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章Points: 1.不认识到犯错,然后得到 ...

  9. [CXF REST标准实战系列] 一、JAXB xml与javaBean的转换(转)

    转自:[CXF REST标准实战系列] 一.JAXB xml与javaBean的转换 文章Points: 1.不认识到犯错,然后得到永久的教训. 2.认识JAXB 3.代码实战 1.不认识到犯错,然后 ...

随机推荐

  1. 机器学习总结之逻辑回归Logistic Regression

    机器学习总结之逻辑回归Logistic Regression 逻辑回归logistic regression,虽然名字是回归,但是实际上它是处理分类问题的算法.简单的说回归问题和分类问题如下: 回归问 ...

  2. [转]Hadoop YARN任务提交流程

    Yarn是随着hadoop发展而催生的新框架,全称是Yet Another Resource Negotiator,可以翻译为“另一个资源管理器”.yarn取代了以前hadoop中jobtracker ...

  3. WEB开发总结(持续更新。。。)

    近期开始搞搞web的东西,觉得有必要把遇到的问题总结一下,就在这里当做个笔记本吧. 1.用maven建立的web工程,在运行的时候,右键找不到“Run on server”菜单: 可以在命令提示行中, ...

  4. jQuery遍历多层json数据

    1.json与jsonp的区别(待查) 2.要遍历的数据如下: {"status": "ok", "code": 200, "da ...

  5. HDU-4631 Sad Love Story 平面最近点对

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4631 数据是随机的,没有极端数据,所以可以分段考虑,最小值是一个单调不增的函数,然后每次分治算平面最近 ...

  6. 我被eclipse的tomcat坑的经历

    奇怪的乱码问题: 1.jsp页面utf-8 2.java代码utf-8 3.数据库编码utf-8 4.tomcat server.xml配置URIEncoding="UTF-8" ...

  7. for循环中使用了return

    for循环中使用了return,导致没有循环完毕就结束了整个方法的执行.

  8. C#学习笔记——面向对象、面向组件以及类型基础

    C#学习笔记——面向对象.面向组件以及类型基础 目录 一 面向对象与面向组件 二 基元类型与 new 操作 三 值类型与引用类型 四 类型转换 五 相等性与同一性 六 对象哈希码 一 面向对象与面向组 ...

  9. iOS Instruments内存检测使用

    Instruments 可以帮我们了解到应用程序使用内存的几个方面: 全局内存使用情况(Overall Memory Use): 从全局的角度监测应用程序的内存使用情况,捕捉非预期的或大幅度的内存增长 ...

  10. java实现生产者消费者问题

    引言 生产者和消费者问题是线程模型中的经典问题:生产者和消费者在同一时间段内共用同一个存储空间,如下图所示,生产者向空间里存放数据,而消费者取用数据,如果不加以协调可能会出现以下情况: 生产者消费者图 ...