Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7014995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:35:41+00:00 2026-05-27T22:35:41+00:00

i’m new to MOXy and JaxB in general and I’m facing a problem with

  • 0

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 !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:35:42+00:00Added an answer on May 27, 2026 at 10:35 pm

    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 an XmlAdapter like the following:

    package forum8745305;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    import javax.xml.bind.annotation.adapters.XmlAdapter;
    
    public class DateAdapter extends XmlAdapter<String, Date> {
    
        private SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    
        @Override
        public String marshal(Date v) throws Exception {
            return dateFormat.format(v);
        }
    
        @Override
        public Date unmarshal(String v) throws Exception {
            return dateFormat.parse(v);
        }
    
    }
    

    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 type java.util.Date belonging to domain classes in that package:

    <?xml version="1.0"?>
    <xml-bindings 
            xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
            version="2.1"
            package-name="forum8745305">
        <xml-java-type-adapters>
            <xml-java-type-adapter value="forum8745305.DateAdapter" type="java.util.Date"/>
        </xml-java-type-adapters>
        <java-types>
            <java-type name="Observation">
                <xml-type prop-order="date theoricalTime ci ch cr type" />
                <xml-root-element/>
                <java-attributes>
                    <xml-element java-attribute="date" xml-path="Date/text()"/>
                    <xml-element java-attribute="theoricalTime" xml-path="TheoricalTime/text()" />
                    <xml-element java-attribute="numeroTrain" xml-path="NumeroTrain/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>
    

    Observation

    Based on your question, below is what your domain class might look like:

    package forum8745305;
    
    import java.util.Date;
    
    public class Observation {
    
        private Date date;
        private Date theoricalTime;
        private String numeroTrain;
        private String ci;
        private String ch;
        private String cr;
        private String type;
    
        public Date getDate() {
            return date;
        }
    
        public void setDate(Date date) {
            this.date = date;
        }
    
        public Date getTheoricalTime() {
            return theoricalTime;
        }
    
        public void setTheoricalTime(Date theoricalTime) {
            this.theoricalTime = theoricalTime;
        }
    
        public String getNumeroTrain() {
            return numeroTrain;
        }
    
        public void setNumeroTrain(String numeroTrain) {
            this.numeroTrain = numeroTrain;
        }
    
        public String getCi() {
            return ci;
        }
    
        public void setCi(String ci) {
            this.ci = ci;
        }
    
        public String getCh() {
            return ch;
        }
    
        public void setCh(String ch) {
            this.ch = ch;
        }
    
        public String getCr() {
            return cr;
        }
    
        public void setCr(String cr) {
            this.cr = cr;
        }
    
        public String getType() {
            return type;
        }
    
        public void setType(String type) {
            this.type = type;
        }
    
    }
    

    Demo

    The following code can be used to run the example:

    package forum8745305;
    
    import java.io.File;
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Marshaller;
    import javax.xml.bind.Unmarshaller;
    
    import org.eclipse.persistence.jaxb.JAXBContextFactory;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            Map<String, Object> properties = new HashMap<String, Object>(1);
            properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, "forum8745305/oxm.xml");
            JAXBContext jc = JAXBContext.newInstance(new Class[] {Observation.class}, properties);
    
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            File xml = new File("src/forum8745305/input.xml");
            Observation observation = (Observation) unmarshaller.unmarshal(xml);
    
            Marshaller marshaller = jc.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(observation, System.out);
        }
    
    }
    

    Input/Output

    <?xml version="1.0" encoding="UTF-8"?>
    <observation>
        <Date>05/01/2012 16:36:24</Date>
        <TheoricalTime>01/02/2012 12:34:45</TheoricalTime>
    </observation>
    

    For More Information

    • http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
    • http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html
    • http://blog.bdoughan.com/search/label/XmlAdapter

    UPDATE

    You can also specify XmlAdapters at the property level. This means you could have a different XmlAdapter for each of your Date properties if you wanted to.

    <?xml version="1.0"?>
    <xml-bindings 
            xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
            version="2.1"
            package-name="forum8745305">
        <java-types>
            <java-type name="Observation">
                <xml-type prop-order="date theoricalTime ci ch cr type" />
                <xml-root-element/>
                <java-attributes>
                    <xml-element java-attribute="date" xml-path="Date/text()">
                        <xml-java-type-adapter value="forum8745305.DateAdapter"/>
                    </xml-element>
                    <xml-element java-attribute="theoricalTime" xml-path="TheoricalTime/text()">
                        <xml-java-type-adapter value="forum8745305.DateAdapter"/>
                    </xml-element>
                    <xml-element java-attribute="numeroTrain" xml-path="NumeroTrain/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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm parsing an XML file, the creators of it stuck in a bunch social
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.