如何使用 MOXy 绑定文件处理 java.util.Date
一般来说,我对 MOXy 和 JaxB 很陌生,并且面临 java.util.Date 转换的问题。
我正在使用映射文件将 XML 文件(我无法控制)解组为对象(我既不能手动注释现有类,也不能更改它们)。
我的 XML 映射文件如下所示:
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
version="2.1">
<java-types>
<java-type name="Observation">
<xml-type prop-order="date theoricalTime ci ch cr type" />
<java-attributes>
<xml-element java-attribute="date" xml-path="Date/text()" />
<xml-element java-attribute="theoricalTime" xml-path="TheoricalTime/text()" />
<xml-element java-attribute="ci" xml-path="CIPR/text()" />
<xml-element java-attribute="ch" xml-path="CHPR/text()" />
<xml-element java-attribute="cr" xml-path="CRPR/text()" />
<xml-element java-attribute="type" xml-path="Type/text()" />
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
在我要编组到的类中,属性“date”和“theoricalTime”的类型为 java.util.Date。
我编组的 xml 中的值是具有这种格式的字符串:“dd/MM/yyyy HH:mm:ss”(“05/01/2012 16:36:24”)。我还有一些只有时间值“HH:mm:ss”(“14:17:33”)的字段。
这是我在解组文件时得到的堆栈跟踪:
Exception in thread "main" Local Exception Stack:
Exception [EclipseLink-3002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.ConversionException
Exception Description: The object [22/01/2009 20:56:29], of class [class java.lang.String], from mapping [org.eclipse.persistence.oxm.mappings.XMLDirectMapping[date-->Date/text()]] with descriptor [XMLDescriptor(Observation --> [DatabaseTable(Observation)])], could not be converted to [class java.util.Calendar].
at org.eclipse.persistence.exceptions.ConversionException.incorrectDateTimeFormat(ConversionException.java:127)
at org.eclipse.persistence.exceptions.ConversionException.incorrectDateTimeFormat(ConversionException.java:133)
at org.eclipse.persistence.internal.oxm.XMLConversionManager.convertStringToXMLGregorianCalendar(XMLConversionManager.java:703)
at org.eclipse.persistence.internal.oxm.XMLConversionManager.convertStringToDate(XMLConversionManager.java:1111)
at org.eclipse.persistence.internal.oxm.XMLConversionManager.convertObjectToUtilDate(XMLConversionManager.java:804)
at org.eclipse.persistence.internal.oxm.XMLConversionManager.convertObject(XMLConversionManager.java:165)
at org.eclipse.persistence.internal.databaseaccess.DatasourcePlatform.convertObject(DatasourcePlatform.java:160)
at org.eclipse.persistence.oxm.mappings.XMLDirectMapping.getAttributeValue(XMLDirectMapping.java:293)
at org.eclipse.persistence.internal.oxm.XMLDirectMappingNodeValue.endElement(XMLDirectMappingNodeValue.java:182)
at org.eclipse.persistence.oxm.record.UnmarshalRecord.endElement(UnmarshalRecord.java:823)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:601)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1774)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2930)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at org.eclipse.persistence.internal.oxm.record.XMLReader.parse(XMLReader.java:157)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:753)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:333)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:320)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:280)
at org.eclipse.persistence.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:306)
at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:115)
at Main.Test(Main.java:97)
at Main.main(Main.java:35)
我的问题是:是否可以使用 MOXy 的外部元数据在映射文件内指定类型转换?如何使用上面指定的格式处理日期时间和时间并将它们映射到日期字段?
(我暗自希望 Blaise Doughan 正在读这篇文章。)
提前感谢您的帮助!
i'm new to MOXy and JaxB in general and I'm facing a problem with java.util.Date conversion.
I'm unmarshaling an XML file (which I have no control of) to objects using a mapping file (I can neither manually annotate existing classes nor change them).
My XML mapping file looks like this :
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
version="2.1">
<java-types>
<java-type name="Observation">
<xml-type prop-order="date theoricalTime ci ch cr type" />
<java-attributes>
<xml-element java-attribute="date" xml-path="Date/text()" />
<xml-element java-attribute="theoricalTime" xml-path="TheoricalTime/text()" />
<xml-element java-attribute="ci" xml-path="CIPR/text()" />
<xml-element java-attribute="ch" xml-path="CHPR/text()" />
<xml-element java-attribute="cr" xml-path="CRPR/text()" />
<xml-element java-attribute="type" xml-path="Type/text()" />
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
In the class which I'm marshalling to, properties "date" and "theoricalTime" are of type java.util.Date.
The values from the xml I'm marshalling from are strings with this kind of format : "dd/MM/yyyy HH:mm:ss" ("05/01/2012 16:36:24"). I also have some fields with only a time value "HH:mm:ss" ("14:17:33").
Here is the stacktrace I'm getting when unmarshalling the file :
Exception in thread "main" Local Exception Stack:
Exception [EclipseLink-3002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.ConversionException
Exception Description: The object [22/01/2009 20:56:29], of class [class java.lang.String], from mapping [org.eclipse.persistence.oxm.mappings.XMLDirectMapping[date-->Date/text()]] with descriptor [XMLDescriptor(Observation --> [DatabaseTable(Observation)])], could not be converted to [class java.util.Calendar].
at org.eclipse.persistence.exceptions.ConversionException.incorrectDateTimeFormat(ConversionException.java:127)
at org.eclipse.persistence.exceptions.ConversionException.incorrectDateTimeFormat(ConversionException.java:133)
at org.eclipse.persistence.internal.oxm.XMLConversionManager.convertStringToXMLGregorianCalendar(XMLConversionManager.java:703)
at org.eclipse.persistence.internal.oxm.XMLConversionManager.convertStringToDate(XMLConversionManager.java:1111)
at org.eclipse.persistence.internal.oxm.XMLConversionManager.convertObjectToUtilDate(XMLConversionManager.java:804)
at org.eclipse.persistence.internal.oxm.XMLConversionManager.convertObject(XMLConversionManager.java:165)
at org.eclipse.persistence.internal.databaseaccess.DatasourcePlatform.convertObject(DatasourcePlatform.java:160)
at org.eclipse.persistence.oxm.mappings.XMLDirectMapping.getAttributeValue(XMLDirectMapping.java:293)
at org.eclipse.persistence.internal.oxm.XMLDirectMappingNodeValue.endElement(XMLDirectMappingNodeValue.java:182)
at org.eclipse.persistence.oxm.record.UnmarshalRecord.endElement(UnmarshalRecord.java:823)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:601)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1774)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2930)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:807)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:107)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
at org.eclipse.persistence.internal.oxm.record.XMLReader.parse(XMLReader.java:157)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:753)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:333)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:320)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:280)
at org.eclipse.persistence.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:306)
at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:115)
at Main.Test(Main.java:97)
at Main.main(Main.java:35)
My question is : Is it possible to specify types conversions inside the mapping file using MOXy's external metadata ? How can I handle datetime and time with the formats specified above and map them to Date fields ?
(I secretly hope Blaise Doughan is reading this.)
Thanks in advance for your help !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
下面演示了如何将
XmlAdapter
与 MOXy 的外部映射文档结合使用来实现您正在寻找的结果:DateAdapter
由于您的日期/时间数据采用以下格式
dd/MM/yyyy HH:mm:ss
您将需要使用XmlAdapter
,如下所示:oxm.xml
这通常在您的域模型使用
@XmlJavaTypeAdapter
注释,但由于您使用的是 MOXy 的外部元数据文档,因此您可以按如下方式指定它。我已在包级别指定它,以便它将应用于属于该包中的域类的java.util.Date
类型的所有字段/属性:观察
基于您的问题,下面是您的域类可能的样子:
演示
以下代码可用于运行示例:
输入/输出
了解更多信息
UPDATE
您还可以在属性级别指定
XmlAdapters
。这意味着如果您愿意,您可以为每个Date
属性使用不同的XmlAdapter
。The following demonstrates how you can use an
XmlAdapter
with MOXy's external mapping document to achieve the results that you are looking for:DateAdapter
Since your date/time data is in the following format
dd/MM/yyyy HH:mm:ss
you will need to use anXmlAdapter
like the following:oxm.xml
This is normally specified on your domain model using the
@XmlJavaTypeAdapter
annotation, but since you are using MOXy's external metadata document you can specify it as follows. I have specified it at the package level so that it will apply to all fields/properties of typejava.util.Date
belonging to domain classes in that package:Observation
Based on your question, below is what your domain class might look like:
Demo
The following code can be used to run the example:
Input/Output
For More Information
UPDATE
You can also specify
XmlAdapters
at the property level. This means you could have a differentXmlAdapter
for each of yourDate
properties if you wanted to.