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 7045611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:33:05+00:00 2026-05-28T02:33:05+00:00

Once again I have a question about Eclipselink/MOXy with external metadata mapping file. I

  • 0

Once again I have a question about Eclipselink/MOXy with external metadata mapping file.

I have a reference xml which applies to a class. This xml contains data that applies to some but not always all the properties that the class can contain.

I also have a custom datetime adapter set for the date fields.

My problem is that the xml I’m unmarshalling does not contain any data for the endDate property, yet when I do this simple test :

  • Unmarshall reference xml to the class
  • Marshall that class to a new xml file
  • Compare the two xml files

That property endDate (which should not be marshalled since it has not been set) is marshalled as 09/01/2012 17:05:28 (it’s always marshalled as a new Date() set to the current time).

Here is a sample XML Metadata file :

<?xml version="1.0"?>
<xml-bindings  xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
                version="2.1">
    <java-types>
        <java-type name="sample.clazz.Task" xml-accessor-type="NONE">
            <xml-root-element name="Task" />
            <xml-type prop-order="startDate endDate id ci ch cr" />
            <java-attributes>
                <xml-element java-attribute="startDate" xml-path="StartDate/text()">
                    <xml-java-type-adapter value="utils.JaxBDateTimeAdapter" type="java.util.Date"/>
                </xml-element>
                <xml-element java-attribute="endDate" required="false" xml-path="EndDate/text()">
                    <xml-java-type-adapter value="utils.JaxBDateTimeAdapter" type="java.util.Date"/>
                </xml-element>
                <xml-element java-attribute="id" xml-path="TaskId/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()" />
            </java-attributes>
        </java-type>
    </java-types>
</xml-bindings>

Here is the class :

package sample.clazz;

public class Task{

    private int id;
    private Date startDate;
    private Date endDate;
    private String ci;
    private String ch;
    private String cr;

    public Task(){

    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public Date getStartDate() {
        return startDate;
    }
    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }
    public Date getEndDate() {
        return endDate;
    }
    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }
    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;
    }

}

Here is my custom DateTimeAdapter :

package utils;

import java.util.Date;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class JaxBDateTimeAdapter extends XmlAdapter<String, Date>  {


    @Override
    public String marshal(Date d) throws Exception {
        if(d != null){
            return DateUtil.getFormatedDateTimeString(d);
        }
        else{
            return null;
        }
    }

    @Override
    public Date unmarshal(String d) throws Exception {
        if(d != null && !"".equals(d)){
            return DateUtil.getDateFromString(d);
        }
        else{
            return null;
        }
    }
}

Here is my reference XML

<?xml version="1.0" encoding="UTF-8"?>
<Task>
    <TaskId>147</TaskId>
    <CRPR>0087</CRPR>
    <CIPR>A683557</CIPR>
    <CHPR>BV</CHPR>
    <StartDate>22/01/2009 20:56:29</StartDate>
</Task>

and Here is the XML I’m getting when re-marshalling the object :

<?xml version="1.0" encoding="UTF-8"?>
<Task>
    <TaskId>147</TaskId>
    <CRPR>0087</CRPR>
    <CIPR>A683557</CIPR>
    <CHPR>BV</CHPR>
    <StartDate>01/01/2012 20:56:29</StartDate>
    <EndDate>09/01/2012 17:05:28</EndDate> <!-- That element should not exist ! -->
</Task>

It seems like Jaxb generates a new date for the empty field, how can I tell him via the external metadata mapping file not to generate nodes for empty or null values ? I tried to set required=false on the metadata file, and I tried testing with my custom DateTimeAdapter if the values were null, but it seems Jaxb creates a new Date object and passes it to the marshal method of the Adapter. I cant think of any way of preventing him to do this.

As for my previous questions, I have no control over the incoming XML’s or the model classes.

Please note : this data is a sample I wrote, it may not be accurate since I cannot expose real data or names, there might be some typing errors.

Thanks 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-28T02:33:06+00:00Added an answer on May 28, 2026 at 2:33 am

    I’m the EclipseLink JAXB (MOXy) lead and I have not been able to reproduce your issue. It may be possible that there is a problem in your DateUtil class. The following is what I have tried:

    oxm.xml

    I made a small change to your metadatafile. Basically I changed it to specify the package name on the xml-bindings element rather than the individual java-type elements:

    <?xml version="1.0"?>
    <xml-bindings  
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        version="2.3"
        package-name="sample.clazz">
        <java-types>
            <java-type name="Task" xml-accessor-type="NONE">
                <xml-root-element name="Task" />
                <xml-type prop-order="startDate endDate id ci ch cr" />
                <java-attributes>
                    <xml-element java-attribute="startDate" xml-path="StartDate/text()">
                        <xml-java-type-adapter value="forum8791782.JaxBDateTimeAdapter" type="java.util.Date"/>
                    </xml-element>
                    <xml-element java-attribute="endDate" required="false" xml-path="EndDate/text()">
                        <xml-java-type-adapter value="forum8791782.JaxBDateTimeAdapter" type="java.util.Date"/>
                    </xml-element>
                    <xml-element java-attribute="id" xml-path="TaskId/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()" />
                </java-attributes>
            </java-type>
        </java-types>
    </xml-bindings>
    

    DateUtil

    You did not provide an implementation of DateUtil in your question, so I used the following. My guess is there is code in your implementation of DateUtil that is causing the output that you are seeing:

    package forum8791782;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class DateUtil {
    
        private static SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    
        public static String getFormatedDateTimeString(Date d) {
            return formatter.format(d);
        }
    
        public static Date getDateFromString(String d) {
            try {
                return formatter.parse(d);
            } catch(Exception e) {
                throw new RuntimeException(e);
            }
        }
    
    }
    

    Demo

    Below is the code I used to run this example. input.xml is the reference XML you cite in your question:

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

    Output

    The following is the output I get from running the sample code. I do not see the EndDate element written out.

    2.3.2.v20111125-r10461
    <?xml version="1.0" encoding="UTF-8"?>
    <Task>
       <StartDate>22/01/2009 20:56:29</StartDate>
       <TaskId>147</TaskId>
       <CIPR>A683557</CIPR>
       <CHPR>BV</CHPR>
       <CRPR>0087</CRPR>
    </Task>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Once again a question about the garbage collector in actionscript-3: If I have a
Once again a question about extjs. I use EXT 4.0.2 and have a couple
Once again I've searched for about 45 minutes for an answer to this question
I have once again fleshed out Ruby, after two years of not touching it,
I need your expertise once again. I have a java class that searches a
Once again a very beginner-ish question, but here I go: I would like to
Hell once again, I am wondering how do you go about adding data to
I'm new here and have a question about C#'s CheckedListBox. I built the CheckedListBox
Let's say you're implementing your own version of stackoverflow (once again, yes) You have
I have a question about the push notification service of the Windows Phone 7

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.