Content: A Value

The content of an XML element may be some value, or one or more subordinate elements, or even a combination of both. Let's look at an XML element with a value of some type. This is defined by a schema construct like this:

<xsd:element name="Quantity" type="xsd:int"/>

This does not define another type, but it may occur as some part of a complex type definition that describes the structure and attributes of the containing element, this element itself and its siblings. Thexsd:element defines the XML tag, so that an example of this XML element is bound to look like this:

<Quantity>144</Quantity>

The Java code resulting from such an embedded element definition is part of some class definition, i.e., the one describing the containing element, and it consists of the declaration of an instance variable, a getter method (here: int getQuantity()) and, except for list types, a setter method (here: void setQuantity(int value)).

An element definition like this may also be provided for specifying the root element of an XML document. Obviously, such an element cannot be part of yet another type definition describing the structure of an enclosing element, simply because there is no such element. The code generated for a stand-alone element definition can be found in the class ObjectFactory which is generated along with all the classes derived from your schema's type definitions. So, from any stand-alone element definition that looks like this

<xsd:element name="Doc" type="DocType"/>

you may expect the generated class ObjectFactory to contain

public class ObjectFactory {
private final static QName _Doc_QNAME = new QName("", "Doc");
//...
public JAXBElement<DocType> createDoc(DocType value) {
return new JAXBElement<DocType>(_Doc_QNAME, DocType.class, null, value);
}
// ...
}

Notice that you are not restricted to a single document root element.

Content: An Ordered Set of Elements

The schema element xsd:sequence defines that the enclosed set of elements should occur in the given order and according to the specified minimum and maximum repetition counts. (The default for both is 1.) The following complex type defines a set of two coordinates.

<xsd:complexType name="PointType">
<xsd:sequence>
<xsd:element name="X" type="xsd:int"/>
<xsd:element name="Y" type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>

The resulting Java code is straightforward.

public class PointType {

    protected int x;
protected int y; public int getX() {
return x;
} public void setX(int value) {
this.x = value;
} public int getY() {
return y;
} public void setY(int value) {
this.y = value;
}
}

Content: An Unordered Set of Elements

Content consisting of a set of elements that may occur in any order within its parent XML element can be defined by using the schema element xsd:all. There is, however, a severe restriction: themaxOccurs may not have a value greater than 1. Here is the definition for an XML element describing the courses of a dinner which does not permit repetitions of any course, but you may omit all courses except for the main dish.

<xsd:complexType name="DinnerType">
<xsd:all>
<xsd:element name="Starter" type="xsd:string" minOccurs="0"/>
<xsd:element name="Soup" type="xsd:string" minOccurs="0"/>
<xsd:element name="Entree" type="xsd:string"/>
<xsd:element name="Dessert" type="xsd:string" minOccurs="0"/>
</xsd:all>
</xsd:complexType>

The generated Java conforms to the structure of a JavaBean:

public class LunchType {

    protected String starter;
protected String soup;
protected String entree;
protected String dessert; public String getStarter() {
return starter;
} public void setStarter( String value ) {
this.starter = value;
} // ...(more getters and setters)
}

Here, the getters for the optional child elements may return null to distinguish "not present" from any possible value.

Content: Alternative Elements

The schema element xsd:choice lets you define a type for an XML element which has a content of exactly one element from a given set of alternatives.

<xsd:complexType name="CommType">
<xsd:choice>
<xsd:element name="SMS" type="xsd:string"/>
<xsd:element name="MMS" type="xsd:string"/>
<xsd:element name="Email" type="xsd:string"/>
</xsd:choice>
</xsd:complexType>

Although only one out of the three elements will actually be present, the generated Java class provides instance variables and getters and setters for all alternatives.

public class CommType {

    protected String sms;
protected String mms;
protected String email; public String getSMS() {
return sms;
} public void setSMS(String value) {
this.sms = value;
} // ...(more getters and setters)
}

Although a handful of unused references isn't all that expensive, a Java class like this just doesn't have a nice feeling about it. Also, using such a class may easily lead to errors that are hard to track down. There is, for instance, nothing in the generated code that will keep you from calling more than one setter.

Content: A Homogeneous List of Elements

To define an element where some sub-element occurs repeatedly, we make use of the optional attributes minOccurs and maxOccurs. Various combinations are possible, permitting the definition of a list that may be empty, may contain any number of elements, or a fixed number. The definition of an unbounded list with at least two elements is given below. (PointType is shown in subsection Content: An Ordered Set of Elements.)

<xsd:complexType name="PolygonType">
<xsd:sequence>
<xsd:element name="Points" type="PointType" minOccurs="2" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>

The resulting Java code does not express the required minimum of two points in a polygon. Here, and for all similar element lists, a java.util.List is used. Therefore the generated code will always be as simple as the one shown below.

public class PolygonType {

    protected List<PointType> points;

    public List<PointType> getPoints() {
if (points == null) {
points = new ArrayList<PointType>();
}
return this.points;
}
}

The Javadoc documentation (omitted here) emphasizes that getPoints returns a reference to the actual list while making sure that the list is created. All methods defined in java.util.List may be applied to the returned value.Most importantly, code like this

polygon.getPoints().add( new PointType( 4, 9 ) );

adds another point. The list can be added to one by one, or you may use addAll for bulk additions, and you could remove or even clear to delete elements. With all of these possibilities there is just no need for a setter for the entire list.

JAXB - XML Schema Types, Defining Types for XML Elements With Content的更多相关文章

  1. 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 ...

  2. 【转】XSD (xml Schema Definition)

    来自:http://www.cnblogs.com/newsouls/archive/2011/10/28/2227765.html Xml Schema的用途 1.  定义一个Xml文档中都有什么元 ...

  3. Xml Schema的用途

    Xml Schema的用途 1.  定义一个Xml文档中都有什么元素 2.  定义一个Xml文档中都会有什么属性 3.  定义某个节点的都有什么样的子节点,可以有多少个子节点,子节点出现的顺序 4.  ...

  4. XML Schema 简介

    XML Schema 是基于 XML 的 DTD 替代者. XML Schema 可描述 XML 文档的结构. XML Schema 语言也可作为 XSD(XML Schema Definition) ...

  5. XML Schema <第三篇>

    验证XML文档是否符合议定的XML结构有两种方法,分别是DTD模式与XML Schema.本文主要介绍XML Schema. 一.XML Schema的优点 XML Schema基于XML,没有专门的 ...

  6. 7 -- Spring的基本用法 -- 11... 基于XML Schema的简化配置方式

    7.11 基于XML Schema的简化配置方式 Spring允许使用基于XML Schema的配置方式来简化Spring配置文件. 7.11.1 使用p:命名空间简化配置 p:命名空间不需要特定的S ...

  7. XML——Schema

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  8. Spring可扩展的XML Schema机制

    可扩展的XML Schema机制 从Spring2.0开始,Spring提供了XML Schema可扩展机制,用户可以自定义XML Schema文件,并自定义XML Bean解析器,并集成到Sprin ...

  9. 什么是 XML Schema(转)

    什么是 XML Schema? XML Schema 的作用是定义 XML 文档的合法构建模块,类似 DTD. XML Schema 的作用: 定义可出现在文档中的元素 定义可出现在文档中的属性 定义 ...

随机推荐

  1. 微软Azure云主机及blob存储的网络性能测试

    http://www.cnblogs.com/sennly/p/4137024.html 微软Azure云主机及blob存储的网络性能测试 1. 测试目的 本次测试的目的在于对微软Azure的云主机. ...

  2. flappy pig小游戏源码分析(3)——解剖util

    这一节我们继续高歌猛进,如果对源码中有无论无何都理解不通的问题,欢迎和我交流,让我也学习一下,我的qq是372402487. 还是按照惯例看看我们的目录结构. 我们在前两节中已经分析了game.js, ...

  3. 可以使用Markdown了?

    园子果然领先 1.标题类 一级标题 二级标题 三级标题 四级 六级 怎么可以用#号?上传上去看看 2.换行 第一行 换一行 在换一行 3.多个下划线 the_odd_egg odd 斜体用星号 4.删 ...

  4. Raspberry Pi无线路由器篇

    RaspberryPi可以折腾的方法很多,我将会吧自己的折腾经验与大家分享.   作为无线路由器,需要提供dhcp的功能和无线ap的能力,我们分别通过isc-dhcp-server和hostapd这两 ...

  5. 关于 Java Collections API 您不知道的 5 件事,第 1 部分

    定制和扩展 Java Collections Java™ Collections API 远不止是数组的替代品,虽然一开始这样用也不错.Ted Neward 提供了关于用 Collections 做更 ...

  6. LDO稳压器工作原理

    LDO稳压器工作原理 随着便携式设备(电池供电)在过去十年间的快速增长,像原来的业界标准 LM340 和LM317 这样的稳压器件已经无法满足新的需要.这些稳压器使用NPN 达林顿管,在本文中称其为N ...

  7. (转)java读取数据库表信息,子段

    import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sq ...

  8. iOS 转让APP

    今天需要将一个客户的app转让到它公司的个人账号上,记录下来流程,给有需要的人方便查看. 上面这个是咨询了苹果的客户之后收到的邮件说明,有需要的大家自己仔细看看. 好了,进入正题: 一:转出方操作 1 ...

  9. linux centos6 NAT 端口转发

    有很多时候我们为了安全,需要将例如数据库服务器放到内网中.但是有些时候又系统给外网开一个端口,这时就可以利用外网的服务器进行一个端口转发.今天我们以centos6 为例进行端口转发配置. 首先vi / ...

  10. 【转】linux中的cut/tr/join/split/xargs命令

    1. cut命令 cut命令用于从文件或者标准输入中读取内容并截取每一行的特定部分并送到标准输出. 截取的方式有三种:一是按照字符位置,二是按照字节位置,三是使用一个分隔符将一行分割成多个field, ...