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;
    }
}
时间: 2024-11-16 12:25:46

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

JAXB - XML Schema Types, Date and Time

JAXB binds all three of the schema types xsd:date, xsd:time and xsd:dateTime to XMLGregorianCalendar. This class is in the package javax.xml.datatype. (Do not confuse this with java.util.GregorianCalendar.) There is a convenient set of methods for ge

JAXB - XML Schema Types, Binary Data

Data that has no "natural" representation with printable characters must, for inclusion in an XML file, still be represented in printable characters. The simple technique for this consists in converting the binary byte values to their hexadecima

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

Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章Points: 1.不认识到犯错,然后得到永久的教训. 2.认识JAXB 3.代码实战 1.不认识到犯错,然后得到永久的教训. 也不是所谓的教训吧,真正的教训来自于对错误的剖析理解很深刻.然后有种"吃一堑,长一智"的感觉才叫教训.近日和团队工头们用CXF3.0和Spring4.0开发一个平台,模仿着第三方支付,用xml进行数据交互

疯狂XML学习笔记(7)-----------XML Schema

XML Schema 是基于 XML 的 DTD 替代者. XML Schema 可描述 XML 文档的结构. XML Schema 语言也可作为 XSD(XML Schema Definition)来引用. 您应当具备的基础知识 在继续学习之前,您需要对下面的知识有基本的了解: HTML / XHTML XML 以及 XML 命名空间 对 DTD 的基本了解 如果您希望首先学习这些项目,请在 首页 访问这些教程. 什么是 XML Schema? XML Schema 的作用是定义 XML 文档

XML详解:第二部分 XML Schema

声明:原创作品,转载时请注明文章来自SAP师太技术博客:www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将追究法律责任!原文链接:http://www.cnblogs.com/jiangzhengjun/p/4290897.html XML Schema. 22 全局/局部声明/定义... 23 模式与名称空间... 23 目标名称空间... 23 局部元素和属性的限定... 24 未声明目标名称空间... 26 在XML实例文档中引用Schema

XML Schema验证

XML Schema验证 一.什么事Schema(XSD) XML Schema是微软定义的一套用来验证XML技术.是一套预先规定的XML元素和属性创建的,这些元素和属性定义了XML文档的结构和内容模式. DTD的局限性: 1.DTD不遵循XML语法. 2.DTD的数据类型有限,与数据库类型不一致. 3.DTD不可以扩展. 4.DTD是不支持命名空间的. Schema的优势: 1.Schema是一种XML语法结构,编写更加方便. 2.Schema可以支持数据类型. 3.Schema是可以扩展的.

XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式

XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式 http://blog.csdn.net/gdjlc/article/details/11374787 2013-09-08 12:16 2824人阅读 评论(0) 收藏 举报 分类: XML(5) 版权声明:本文为博主原创文章,未经博主允许不得转载. XML Schema 语言也称作 XML Schema 定义(XML Schema Definition,XSD),作用是定义 XML 文档的合

Xml Schema:C#访问在complextype中插入新元素

最近用c#写Xml Schema代码,找了很久也找不到如何在已有的complextype中插入新的element,最后我充分发挥自己的聪明才智,哈哈,终于从...中找到了灵感. XmlSchemaSet xmlSSet; foreach( XmlSchema schema in xmlSSet.Schemas()) { XmlSchemaElement xmlSchRootElement = (XmlSchemaElement )schema.Items[0];//schema里面只能看到根节点

XML Schema编程快速入门

一.XML Schema简介 XML Schema 使用一套预先定义好元素和属性 二.XML Schema的定义和使用 1.编写数据XML文件 2.编写Schema文件 .xsd 引入W3C名称空间:xmlns="http://www.w3.org/2001/XMLSchema" 编写schema内容,对每一个元素,用一个<elementname=""></element>表示,包含其他元素的元素称为复杂元素,需要加<complexTy