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 ...
随机推荐
- 命令cd
"." 当前目录".." 上一级目录"~" 用户家目录 cd + 回车 回到家目录“-” 上一个工作目录
- poj 1704 阶梯博弈
转自http://blog.sina.com.cn/s/blog_63e4cf2f0100tq4i.html 今天在POJ做了一道博弈题..进而了解到了阶梯博弈...下面阐述一下我对于阶梯博弈的理解. ...
- Hadoop概念学习系列之URI深入(三十二)
ls / ------------------------ 这是查本地Linux上的根 hadoop fs -ls / ------------- 这是查hdfs上的根 或者, had ...
- hdoj 2120 Ice_cream's world I【求成环数】
Ice_cream's world I Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- nyoj 811 变态最大值
变态最大值 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 Yougth讲课的时候考察了一下求三个数最大值这个问题,没想到大家掌握的这么烂,幸好在他的帮助下大家算是解 ...
- 框架中web.xml中配置文件解析
1.XSS指跨站脚本攻击 xss表示Cross Site Scripting(跨站脚本攻击),它与SQL注入攻击类似,SQL注入攻击中以SQL语句作为用户输入,从而达到查询/修改/删除数据的目的,而在 ...
- A Mathematician‘s Survival Guide Graduate School and Early Career Development
推荐大家一本书尤其是即将读研究生或者研一研二的学生: A Mathematician‘s Su ...
- eclipse项目导入到Android Studio Plugin with id 'android-library' not found
在主项目的build.gradle 中加入以下代码buildscript { repositories { mavenCentral() } dependencies { classpath 'com ...
- CentOS6.2下安装eclipse
在eclipse官网下载eclipse的linux版本(此处省略下载过程),我下载的是eclipse-jee-indigo-SR2-linux-gtk.tar.gz,下面是安装过程: 1.sudo ...
- c/c++中使用指针需要注意的问题
一.使用指针的时候需要注意几点: 分配空间 初始化 释放 二.常见的错误有几种: 1)内存分配未成功,却使用了它 编程新手常犯这种错误,因为他们没有意识到内存分配会不成功.常用解决办法是,使 ...