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.不认识到犯错,然后 ...
随机推荐
- WAMP下 搭建Zend Framework(Hello World)实例
一.环境搭建 1.下载安装Zend Framework,此步骤不再详细叙述 2.配置php.ini文件 打开php.ini文件,可以找到如下的代码: ; Windows: "\path1;\ ...
- java对象和类学习
定义对象的类: 一个对象的状态(属性或特征)是指那些具有他们当前值的数据域 一个对象的行为是由方法定义的,调用对象的方法就是完成对象的一个动作 使用一个通用类来定义同一类型的对象.类是一个模板,一个对 ...
- Mac java环境配置
进入命令行 cd ~ touch .bash_profile vi .bash_profile 输入内容jdk变量配置内容: export JAVA_HOME=/Library/Java/JavaVi ...
- 通过dbcc page来查看表中的数据
--1.先建表 CREATE TABLE test(idd INT NOT NULL,name VARCHAR(10) NULL) INSERT INTO TEST SELECT 1,'abcdefg ...
- android模拟器访问localhost或127.0.0.1报错
在一般的Java Web程序开发中,我们通常使用localhost或者127.0.0.1来访问本机的Web服务,但是如果我们在Android模拟器中也采用同样的地址来访问,Android模拟器将无法正 ...
- Codeforces 390Div2-754D. Fedor and coupons(贪心+优先队列)
D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- MSSQLSERVER数据库- 慎用SELECT INTO复制表
很多时候我们习惯于用SELECT INTO复制一个表或表结构,因为它方便,快捷,而且在某些情况下效率比INSERT INTO 效率要高一些.但是要注意: SELECT INTO 复制表或表结构的时候, ...
- [一位菜鸟的COCOS-2D编程之路]精灵表单的制作以及简易动画的生成
1.第一步:使用Zwoptex 制作精灵表单 2.制作的表单的名称为 cocos2Dpng,cocos2D.plist: 3.精灵的动画效果 主要分为五部分. // on "init&quo ...
- Attribute特性验证模型model
数据验证我们往往分为前台验证和后台验证,而我们的后台验证每到一个方法中就要去验证一次,这样的代码想想都难以维护,这篇我们这篇文章就是为了解决这样的问题.用attribute 这个特性来解决这样的问题 ...
- Eclipse+Maven构建web项目及部署时Maven lib依赖问题的解决
目录 Eclipse中m2e插件构建web项目的步骤 Maven工具构建web项目再导入Eclipse的步骤 [一].Eclipse中m2e插件构建web项目的步骤 第一步:创建项目,按照 New – ...