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 ...
随机推荐
- javascript活动对象的理解——伪单例模式
在自己研究javascript各种设计模式的过程中,偶然写出的一段代码让自己理解的更深刻了,之所以称之为伪单例模式,是因为这段代码造成的结果很想单例模式,但是实际上是活动对象捣乱所造成的误会. 代码很 ...
- HW6.19
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- JS实现复制到剪贴板
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- ALM11需求和测试覆盖率图解1
覆盖率分析视图 需求覆盖率选项卡
- Jquery ajax 得到返回值
Jquery ajax 得到返回值 1.ajax默认是异步调用的,所以得到的返回值是空值,要得到值必须改成同步:async: false,//同步. 2.必须定义一个全局变量 var result = ...
- Stage3D学习笔记(一):3D术语简介
网格(Mesh) 3D中,所有可见的模型都被称作网格.网格是由3DMax等建模软件制作,定义了一个3D物体的形状.一个网格是由多个多边形组成的. 多边形(Polygon) 一个多边形是组成网格的一个最 ...
- eclipse+webservice开发实例
1.參考文献: 1.利用Java编写简单的WebService实例 http://nopainnogain.iteye.com/blog/791525 2.Axis2与Eclipse整合开发Web ...
- github fork项目后,代码更新
协助约定 每个人都可以fork一份自己的repo,所有的修改都在自己私有的repo上进行:修改完成,测试通过后通过给主repo发pull request请求合并:主repo(Johnqing/n.js ...
- Moving From Objective-C to C++
1. virtual method or not: It's better to declare all cpp member methods without "virtual" ...
- Routes
Routes Routing lets you create your own URL paths, based on the path you can load a closure or a con ...