JAXB - XML Schema Types, Date and Time
2016-05-18 13:16
381 查看
JAXB binds all three of the schema types
The XML schema snippet shown below defines an element containing sub-elements with
The generated class contains the usual getters and setters:
However, some work remains to be done before we can call either setter. It's the class
You may have noticed the
The XML element produced by this code will look like this:
You should notice that the date and time representations follow ISO 8601.
xsd:date,
xsd:timeand
xsd:dateTimeto
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
XMLGregorianCalendaris 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:dateand
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.DatatypeFactorythat provides the methods with which we can create the
javax.xml.datatype.XMLGregorianCalendarobjects.
// 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
nullargument in the method constructing an
XMLGregorianCalendarwith 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.
相关文章推荐
- ListView的性能优化之convertView和viewHolder
- hjr-DSP:CMD文件
- 学会进程管理,人生迎来新高度,&, [Ctrl]-z, fg, bg, kill, killall, nohup
- 【wpf WebBrowser 清空网站的Cookie&Session 清空用户登录状态】
- Volley
- Spark性能相关参数配置详解-任务调度篇
- hibernate_detachedCateria_Disjunction(多条件or)
- MySQL导入.sql文件以及#1146 - Table 'hibernate.person_email' doesn't exist
- [剑指offer]删除链表中重复的结点
- 历年错题集2015
- cmd.ExecuteNonQuery()、cmd.ExecuteScalar()、cmd.ExecuteReader()
- 修改键值映射
- Material Design之CollapsingToolbarLayout 相关属性和方法介绍
- 腾讯微云下线同步盘功能
- 开源许可证GPL、BSD、MIT、Mozilla、Apache和LGPL的区别
- java实现抓取某公司官网新闻
- C/C++中的switch使用
- 枚举(enum)
- 创业的学费 99%不是在哈佛里交的
- Redis操作的封装类