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 getting at the various components such as year or day or minute. But creating any of these values isn't quite so simple because XMLGregorianCalendar
is an abstract class. We'll illustrate this with a simple example for marshalling date and time.
The XML schema snippet shown below defines an element containing sub-elements with xsd:date
and xsd:time
.
<xsd:complexType name="DateTimeType">
<xsd:sequence>
<xsd:element name="Date" type="xsd:date"/>
<xsd:element name="Time" type="xsd:time"/>
</xsd:sequence>
</xsd:complexType>
The generated class contains the usual getters and setters:
public class DateTimeType { protected XMLGregorianCalendar date;
protected XMLGregorianCalendar time; public XMLGregorianCalendar getDate() {
return date;
} public void setDate(XMLGregorianCalendar value) {
this.date = value;
} public XMLGregorianCalendar getTime() {
return time;
} public void setTime(XMLGregorianCalendar value) {
this.time = value;
}
}
However, some work remains to be done before we can call either setter. It's the class javax.xml.datatype.DatatypeFactory
that provides the methods with which we can create the javax.xml.datatype.XMLGregorianCalendar
objects.
// Create a DateTimeType element for the current time and date.
ObjectFactory of = new ObjectFactory();
DateTimeType meta = of.createDateTimeType();
GregorianCalendar now = new GregorianCalendar(); // Obtain a DatatypeFactory instance.
DatatypeFactory df = DatatypeFactory.newInstance(); // Create an XMLGregorianCalendar with the current date.
XMLGregorianCalendar gcDate =
df.newXMLGregorianCalendarDate(
now.get( Calendar.YEAR ),
now.get( Calendar.MONTH ),
now.get( Calendar.DAY_OF_MONTH ),
DatatypeConstants.FIELD_UNDEFINED ); // Create an XMLGregorianCalendar with the current time.
XMLGregorianCalendar gcTime =
df.newXMLGregorianCalendarTime(
now.get( Calendar.HOUR_OF_DAY ),
now.get( Calendar.MINUTE ),
now.get( Calendar.SECOND ),
null, // no fraction
DatatypeConstants.FIELD_UNDEFINED ); // Insert sub-elements into the DateTimeType element.
meta.setDate( gcDate );
meta.setTime( gcTime );
You may have noticed the null
argument in the method constructing an XMLGregorianCalendar
with the time. This indicates that we don't care about fractions of seconds. It is, however, not possible to omit seconds entirely.
The XML element produced by this code will look like this:
<DateTime>
<Date>2008-07-23</Date>
<Time>18:42:24</Time>
</DateTime>
You should notice that the date and time representations follow ISO 8601.
JAXB - XML Schema Types, Date and Time的更多相关文章
- JAXB - XML Schema Types, Defining Subtypes
Although object orientation isn't a key feature of XML or the XML Schema language, it's still possib ...
- JAXB - XML Schema Types, Binary Data
Data that has no "natural" representation with printable characters must, for inclusion in ...
- 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 ...
- XML Schema and XMLspy notes
Introduction An xml documents consists of elements, attributes and text. There are two structures in ...
- XSD(XML Schema Definition)用法实例介绍以及C#使用xsd文件验证XML格式
XML Schema 语言也称作 XML Schema 定义(XML Schema Definition,XSD),作用是定义 XML 文档的合法构建模块,类似 DTD,但更加强大. 作用有: ①定义 ...
- 【转】XSD (xml Schema Definition)
来自:http://www.cnblogs.com/newsouls/archive/2011/10/28/2227765.html Xml Schema的用途 1. 定义一个Xml文档中都有什么元 ...
- [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 ...
随机推荐
- leetcode–Binary Tree Maximum Path Sum
1.题目说明 Given a binary tree, find the maximum path sum. The path may start and end at any node in t ...
- Java字符流读写数据的两种方式
第一种方式:逐个字符进行读写操作(代码注释以及详细内容空闲补充) package IODemo; import java.io.FileReader; import java.io.FileWrite ...
- 第九章、文件与文件系统的压缩与打包 3. 打包命令: tar
打包命令: tar gzip 与 bzip2 也能够针对目录来进行压缩, 不过,这两个命令对目录的压缩指的是『将目录内的所有文件 "分别" 进行压缩』! 将多个文件或目录包成一个大 ...
- loadmore
实例点击 loadmore.js /* * loadmore.js require jQuery,artTemplate * Butterfly 2013.08.28 */ define(['../u ...
- 牛一网ecshop综合类模板(仿淘常州) for ecshop 2.7.3
牛一网目前已经开发好这款ecshop综合类/多色系模板,已经开发好多种色系,本模板将会搭配多种颜色让用户选择,以适应 家电数码. 个护化妆. 生鲜水果 . 服饰鞋帽 . 图书音像 . 粮油食品.家具家 ...
- HDU 1561 树形DP(入门)
题目链接: HDU 1561 The more, The Better #include <iostream> #include <cstdio> #include < ...
- 数学之路(3)-机器学习(3)-机器学习算法-SVM[5]
svm小结 1.超平面 两种颜色的点分别代表两个类别,红颜色的线表示一个可行的超平面.在进行分类的时候,我们将数据点 x 代入 f(x) 中,如果得到的结果小于 0 ,则赋予其类别 -1 ,如果 ...
- cas 3.5.2 登录成功后,如何返回用户更多信息?
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- 看文章《EAI和SOA的比较》有感(1)
<EAI和SOA的比较>http://www.cnblogs.com/asdling/archive/2007/11/26/973100.html这篇文章写的很全面,至少自己这么认为,也解 ...
- BZOJ 1934: [Shoi2007]Vote 善意的投票 最小割
1934: [Shoi2007]Vote 善意的投票 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnl ...