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 ...
随机推荐
- .NET通用基本权限系统
DEMO下载地址: http://download.csdn.net/detail/shecixiong/5372895 一.开发技术:B/S(.NET C# ) 1.Windows XP以上 (支援 ...
- input输入密码变黑点密文
input输入密码加密 html代码 <form id="login-form" method="post" onsubmit="return ...
- 【Stage3D学习笔记续】真正的3D世界(二):显示模型
虽然我们进入真3D世界了,但是上一章的Demo仍然是显示的一个平面,尽管我们的平面在3D空间中旋转可以看出一点3D透视的效果,但是既然是真3D,就要拿出点3D的样子来! 如果要显示3D模型,我们就要告 ...
- 代码自动生成工具MyGeneration之一(程序员必备工具)
代码自动生成工具MyGeneration之一(程序员必备工具) 转 分类: C#2008-08-06 18:12 16064人阅读 评论(12) 收藏 举报 工具数据库相关数据库stringbrows ...
- XMIND
XMind 是一款非常实用的商业思维导图软件,应用全球最先进的Eclipse RCP 软件架构,全力打造易用.高效的可视化思维软件,强调软件的可扩展.跨平台.稳定性和性能,致力于使用先进的软件技术帮助 ...
- 【IBM】使用 CAS 在 Tomcat 中实现单点登录
来源: IBM Developer http://www.ibm.com/developerworks/cn/opensource/os-cn-cas/ 张 涛 (zzhangt@cn.ibm.com ...
- hiberante中get和load方法的区别
1.从返回结果上对比: load方式检索不到的话会抛出org.hibernate.ObjectNotFoundException异常 get方法检索不到的话会返回null 2.从检索执行机制上对比: ...
- 用HTML5 Canvas 做擦除及扩散效果
2013年的时候曾经使用canvas实现了一个擦除效果的需求,即模拟用户在模糊的玻璃上擦除水雾看到清晰景色的交互效果.好在2012年的时候学习HTML5的时候研究过canvas了,所以在比较短的时间内 ...
- 从零开始学android开发-项目debug
在红框处能看到变量值
- 网络防火墙实战-基于pfsense(1)
pfSense是一个免费的,开源的FreeBSD作为防火墙和路由器专为使用自定义版本.除了 是一个功能强大的,灵活的防火墙和路由平台,它包括一个长长的清单相关的功能,并允许进一步的扩展包,不添加膨胀和 ...