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 ...
随机推荐
- MyEclipse整合Git
1. 在OSC@China申请账号,建立项目 2. MyEclipse中选择导入项目-->Git-->Projects from Git 3. 填入Git的地址.User Name和Pas ...
- soliworks三维机柜布局(四)进入solidworks中三维布线
首先需要在solidworks electrical中创建solidworks装配体文件. 菜单栏:处理--solidworks机柜布局--勾选要创建的装配体--点击确定 在文件列表下右键装配体文件- ...
- PT100测温函数
PT100电阻值计算过程如下: 理论电压关系为:V3-V1=11(V2-V1).由于电阻等的误差原因,采用实际测量求平均值的方法得出实际放大倍数. 放大电路测量几组数据如下:其中V3-V1=Av(V2 ...
- [iOS基础控件 - 5.1] UIScrollView
A.需要掌握 UIScrollView 是一个能够滚动的视图控件,可以用来展示大量内容,如手机的“设置” 1.常见属性 2.常用代理方法 3.缩放 4.UIScrollView和UIPageContr ...
- SQLite使用教程6 创建表
http://www.runoob.com/sqlite/sqlite-create-table.html SQLite 创建表 SQLite 的 CREATE TABLE 语句用于在任何给定的数据库 ...
- c/c++中使用指针需要注意的问题
一.使用指针的时候需要注意几点: 分配空间 初始化 释放 二.常见的错误有几种: 1)内存分配未成功,却使用了它 编程新手常犯这种错误,因为他们没有意识到内存分配会不成功.常用解决办法是,使 ...
- 【22】将成员变量声明为private
1.为什么要将成员变量声明为private,语法一致性,只通过方法暴露接口. 2.使用方法,可以对成员变量更精确的控制.比如:为所有可能的实现提供弹性,不同实现可以替换:控制可读可写:验证约束条件:处 ...
- 【转】C++ 笔试面试题目
原文:http://blog.csdn.net/txgc1009/article/details/6700830 许多面试题看似简单,却需要深厚的基本功才能给出完美的解答.企业要求面试者写一个最简单的 ...
- 第二周:01 ICP迭代交互
本周主要任务01:利用PCL库函数,ICP融合两个角度的点云 任务时间:2014年9月8日-2014年9月14日 任务完成情况:可以使用键盘交互,显示每次ICP迭代结果 任务涉及基本方法: 1.PCL ...
- ios开发——实用技术OC-Swift篇&本地通知与远程通知详解
本地通知与远程通知详解 一:本地通知 Local Notification的作用 Local Notification(本地通知) :是根据本机状态做出的通知行为,因此,凡是仅需依赖本机状态即可判 ...