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:
ItemTypeadds 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的更多相关文章
- 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 ...
 - 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 ...
 - 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 ...
 - JAXB - XML Schema Types, Date and Time
		
JAXB binds all three of the schema types xsd:date, xsd:time and xsd:dateTime to XMLGregorianCalendar ...
 - JAXB - XML Schema Types, Binary Data
		
Data that has no "natural" representation with printable characters must, for inclusion in ...
 - XML Schema and XMLspy notes
		
Introduction An xml documents consists of elements, attributes and text. There are two structures in ...
 - [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 ...
 - [CXF REST标准实战系列] 一、JAXB xml与javaBean的转换
		
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章Points: 1.不认识到犯错,然后得到 ...
 - [CXF REST标准实战系列] 一、JAXB xml与javaBean的转换(转)
		
转自:[CXF REST标准实战系列] 一.JAXB xml与javaBean的转换 文章Points: 1.不认识到犯错,然后得到永久的教训. 2.认识JAXB 3.代码实战 1.不认识到犯错,然后 ...
 
随机推荐
- mysql的group_concat的用法
			
1.语法:group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符']) eg. SELECT ID, GROUP ...
 - 关于java对象的思考
			
不可变对象和类 由不可变类创建的对象就是不可变对象,要使一个类成为不可变的,它必须满足下面的需求: 所有数据域都是私有的 没有修改器方法 没有一个访问器的方法,它会返回一个指向可变数据域的引用 看下面 ...
 - OpenCV中的常用函数
			
1.cvLoadImage:将图像文件加载至内存: 2.cvNamedWindow:在屏幕上创建一个窗口: 3.cvShowImage:在一个已创建好的窗口中显示图像: 4.cvWaitKey:使程序 ...
 - [MySQL优化案例]系列 — slave延迟很大优化方法
			
备注:插图来自网络搜索,如果觉得不当还请及时告知 :) 一般而言,slave相对master延迟较大,其根本原因就是slave上的复制线程没办法真正做到并发.简单说,在master上是并发模式(以In ...
 - mysql登陆报错(ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2))
			
部署mysql版本信息 version: 5.6.21 具体现象: mysql服务能够正常启动如下: [root@localhost ~]# service mysqld restart Shutti ...
 - CentOS安装卸载memcache及JAVA示例
			
原文地址:http://www.cnblogs.com/zhongshengzhen/ 先安装libevent,memcached依赖libevent的lib [root@VM_64_81_c ...
 - GridView控件显示图片
			
与图片的二进制数据库存储和显示 1.将图片以二进制存入数据库 2.读取二进制图片在页面显示 3.设置Image控件显示从数据库中读出的二进制图片 4.GridView中ImageField以URL方式 ...
 - 剑指OFFER之最小的K个数(九度OJ1371)
			
题目描述: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. 输入: 每个测试案例包括2行: 第一行为2个整数n,k(1< ...
 - uva 11246 - K-Multiple Free set(数论)
			
题目链接:uva 11246 - K-Multiple Free set 题目大意:给定n,k.求一个元素不大于n的子集,要求该子集的元素尽量多,而且不含两个数满足a∗k=b. 解题思路:容斥原理.f ...
 - Codeforces Round #261 (Div. 2)  D 树状数组应用
			
看着题意:[1,i]中等于a[i]的个数要大于[,jn]中等于a[j]的个数 且i<j,求有多少对这种(i,j) ,i<j可是 i前面的合法个数 要大于j后面的 看起来非常像逆序数的样子 ...