Unmarshalling in JAXB - jaxb

I have the following tester.xml file which contains some info about events.
<?xml version="1.0"?>
<resultset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
<field name="esid">539661</field>
<field name="esname">Title 01</field>
<field name="eslink">http://www.some_event_link.com</field>
<field name="estext">Event description 01</field>
<field name="esinfo" xsi:nil="true" />
<field name="espicture_small">http://www.some_event_link.com/media/small_image.jpg</field>
<field name="espicture">http://www.some_event_link.com/media/some_image..gif</field>
<field name="espicture_big">http://www.some_event_link.com/media/big_image.jpg</field>
<field name="esbegin">2000-11-22</field>
<field name="esend">2011-12-15</field>
<field name="eventid">1379305</field>
<field name="eventname">Event name 01</field>
<field name="eventdate">2011-10-12</field>
<field name="eventtime">19:00:00</field>
<field name="eventlink">http://www.mysite.com/tickets.html</field>
<field name="eventvenue">Event venue 01</field>
</row>
<row>
<field name="esid">539636</field>
<field name="esname">Title 02</field>
<field name="eslink">http://www.some_event_link.com</field>
<field name="estext">Event description 02</field>
<field name="esinfo" xsi:nil="true" />
<field name="espicture_small">http://www.some_event_link.com/media/small_image.jpg</field>
<field name="espicture">http://www.some_event_link.com/media/some_image..gif</field>
<field name="espicture_big">http://www.some_event_link.com/media/big_image.jpg</field>
<field name="esbegin">2000-10-10</field>
<field name="esend">2011-11-01</field>
<field name="eventid">1379081</field>
<field name="eventname">Event name 01</field>
<field name="eventdate">2011-10-12</field>
<field name="eventtime">14:00:00</field>
<field name="eventlink">http://www.mysite.com/tickets.html</field>
<field name="eventvenue">Event venue 02</field>
</row>
Also I have my XML mapping classes as follows.
First is the class corresponding to the tag < resultset >
package com.wapice.xml.beans;
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "resultset")
public class Resultset {
private ArrayList<Row> rowsList;
#XmlElement(required = true, name = "row")
public ArrayList<Row> getRowsList() {
return rowsList;
}
public void setRowsList(ArrayList<Row> rowsList) {
this.rowsList = rowsList;
}
}
Next is the class which corresponds to the tag < row >
package com.wapice.xml.beans;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
#XmlRootElement(name = "row")
#XmlType(propOrder = {"field"})
public class Row {
private String field;
#XmlElement(required = true, name = "field")
public String getField() {
return field;
}
public void setField(String field) {
this.field = field;
}
}
I tried to Unmarshall this xml into objects & print the field names & values on my console with the following code fragment.
try {
JAXBContext context = JAXBContext.newInstance(Resultset.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Resultset resultSet = (Resultset)unmarshaller.unmarshal(new FileReader("tester.xml"));
for(Row row : resultSet.getRowsList()){
System.out.println("Field : " +row.getField());
}
}
catch (JAXBException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
But when I run the above code, it only prints the last field's value. The output is as follows.
Field : Event venue 01
Field : Event venue 02
Can someone please tell me what I'm doing wrong here & it would be much appreciated if someone could tell me how to print all my < field > along with their names & values.
Thanks in advance.
Asela.

You could introduce a Field object:
package com.wapice.xml.beans;
import javax.xml.bind.annotation.*;
public class Field {
#XmlAttribute name;
#XmlValue value;
}
And have the Row object hold onto a List of them:
package com.wapice.xml.beans;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
#XmlRootElement(name = "row")
#XmlType(propOrder = {"field"})
public class Row {
private List<Field> fields;
#XmlElement(required = true, name = "field")
public List<Field> getFields() {
return field;
}
public void setField(List<Field> fields) {
this.fields = fields;
}
}
For More Information
http://blog.bdoughan.com/2010/09/jaxb-collection-properties.html
http://blog.bdoughan.com/2011/06/jaxb-and-complex-types-with-simple.html

I managed to solve my issue with your post & it was really really helpful. Thank you so very much for that. However I had to do some modifications to get it working as my mapping classes were throwing the following exceptions.
com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 2 counts of IllegalAnnotationExceptions
Class has two properties of the same name "rowsList"
this problem is related to the following location:
at public java.util.ArrayList com.wapice.xml.beans.Resultset.getRowsList()
at com.wapice.xml.beans.Resultset
this problem is related to the following location:
at private java.util.ArrayList com.wapice.xml.beans.Resultset.rowsList
at com.wapice.xml.beans.Resultset
Class has two properties of the same name "fieldsList"
this problem is related to the following location:
at public java.util.ArrayList com.wapice.xml.beans.Row.getFieldsList()
at com.wapice.xml.beans.Row
at private java.util.ArrayList com.wapice.xml.beans.Resultset.rowsList
at com.wapice.xml.beans.Resultset
this problem is related to the following location:
at private java.util.ArrayList com.wapice.xml.beans.Row.fieldsList
at com.wapice.xml.beans.Row
at private java.util.ArrayList com.wapice.xml.beans.Resultset.rowsList
at com.wapice.xml.beans.Resultset
Then I changed the names of the related getters/setters & it worked fine.
Following is how I changed it.
----------------
Class Resultset
----------------
#XmlElement(required = true, name = "row")
private ArrayList<Row> rowsList; // I kept the same name for this attribute
public ArrayList<Row> getRowsList() { // I changed this to getRows()
return rowsList;
}
public void setRowsList(ArrayList<Row> rowsList) { // I changed this to setRows()
this.rowsList = rowsList;
}
----------
Class Row
----------
#XmlElement(required = true, name = "field")
private ArrayList<Field> fieldsList; // I kept the same name for this attribute
public void setFieldsList(ArrayList<Field> fieldsList) { // I changed this to getFields()
this.fieldsList = fieldsList;
}
public ArrayList<Field> getFieldsList() { // I changed this to setFields()
return fieldsList;
}
Hope this helps someone else too.

Related

Jaxb eclipse moxy Nullable in element and XmlAdapter with Map<String,Date>

This is my code according the example:
I was looking for formatting the Date field inside a Map
but it throws this exception:
The object [{birthDate=1963-07-16 00:00:00.0}], of class [class org.hibernate.collection.internal.PersistentMap], could not be converted to [class it.cineca.jaxb.adapter.MyMapType].
Example
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public final class MyMapAdapter extends
XmlAdapter<MyMapType,Map<String, Date>> {
private SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
#Override
public MyMapType marshal(Map<String, Date> arg0) throws Exception {
MyMapType myMapType = new MyMapType();
for(Entry<String, Date> entry : arg0.entrySet()) {
MyMapEntryType myMapEntryType =
new MyMapEntryType();
myMapEntryType.key = entry.getKey();
myMapEntryType.value = dateFormat.parse(entry.getValue().toString());
myMapEntryType.value = entry.getValue();
myMapType.entry.add(myMapEntryType);
}
return myMapType;
}
#Override
public Map<String, Date> unmarshal(MyMapType arg0) throws Exception {
HashMap<String, Date> hashMap = new HashMap<String, Date>();
for(MyMapEntryType myEntryType : arg0.entry) {
hashMap.put(myEntryType.key, myEntryType.value);
}
return hashMap;
}
}
import java.util.Date;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlValue;
public class MyMapEntryType {
#XmlAttribute
public String key;
#XmlValue
public Date value;
}
import java.util.ArrayList;
import java.util.List;
public class MyMapType {
public List<MyMapEntryType> entry =
new ArrayList<MyMapEntryType>();
}
this is the person-binding file
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="it.model" xml-mapping-metadata-complete="true">
<xml-schema
element-form-default="QUALIFIED"/>
<java-types>
<java-type name="Person" xml-accessor-type="NONE">
<xml-root-element/>
<xml-type prop-order="firstName lastName stringMap "/>
<java-attributes>
nillable="true"/>
<xml-element java-attribute="stringMap" name="string-map" nillable="true"/>
<xml-element java-attribute="dateMap" name="date-map" nillable="true">
<xml-java-type-adapter value="it.cineca.jaxb.adapter.MyMapAdapter" />
</xml-element>
<xml-element java-attribute="positionCurrentSet" name="position-current-set" nillable="true">
<!-- UNLIKELY THIS DOESN'T PRODUCE EMPTY NODE -->
<xml-null-policy xsi-nil-represents-null="true" empty-node-represents-null="true" null-representation-for-xml="EMPTY_NODE" is-set-performed-for-absent-node="true" />
</xml-element>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
So How Could I solve It?
I tried following the http://blog.bdoughan.com/2010/07/xmladapter-jaxbs-secret-weapon.html
post but I don't find the bug it's the same logic.
I think you have the parameter types reversed in MyMapAdapter. From the JAXB javadocs:
public abstract class XmlAdapter<ValueType,BoundType>
BoundType - The type that JAXB doesn't know how to handle. An adapter is written to allow this type to be used as an in-memory representation through the ValueType.
ValueType - The type that JAXB knows how to handle out of the box.
Try changing your class signature to MyMapAdapter extends XmlAdapter<Map<String, Date>, MyMapType>, and swap your marshal and unmarshal methods.
Hope this helps,
Rick

jaxb unmarshalling creates duplicate list of objects : How to unmarshall objects in lists?

jaxb unmarshalling creates duplicate list of objects : How to unmarshall objects in lists?
xml file and code is shown below while unmarshalling reading elements getting duplicate list i am using jaxb annotations and my final out put is duplicate list
<data_reading>
<load_survey>
<interval_settings value="30" xunit="mins" />
<measurement name="energy_real" xunit="KWH" />
<reading interval="00" value="000.010" />
<reading interval="01" value="000.000" />
<reading interval="02" value="000.050" />
<reading interval="03" value="000.080" />
<reading interval="04" value="000.010" />
</load_survey>
</data_reading>
These are my classes
#XmlAccessorType(XmlAccessType.FIELD)
public class LoadSurvey {
#XmlElement(name="interval_settings")
private IntervalSettings interval_settings;
#XmlElement(name="measurement")
private Measurement measurement;
#XmlElement(name="reading", type = Reading.class)
private List<Reading> readings;
//setter and getters
}
#XmlRootElement(name="data_reading")
#XmlAccessorType(XmlAccessType.FIELD)
public class DataReading {
#XmlElement(name="load_survey")
private LoadSurvey load_survey;
}
This is my code Here i am getting following output
[data_reading [load_survey=LoadSurvey [interval_settings=IntervalSettings [value=30, xunit=mins], measurement=Measurement [name=energy_real, xunit=KWH], readings=[Reading [interval=00, value=0.23], Reading [interval=01, value=0.23], Reading [interval=02, value=0.22], Reading [interval=03, value=0.21], Reading [interval=04, value=0.23], Reading [interval=05, value=0.24], Reading [interval=00, value=0.23], Reading [interval=01, value=0.23], Reading [interval=02, value=0.22], Reading [interval=03, value=0.21], Reading [interval=04, value=0.23], Reading [interval=05, value=0.24]]]]
Getting Readings list duplicates with jaxb please provide any solution
The only way you could be getting duplicate items in the list is if you have mapped both the field (instance) variable and the corresponding property (get/set method). Since you have specified XmlAccessType.FIELD make sure you have not annotated the get method for the list property.
For More Information
http://blog.bdoughan.com/2010/09/jaxb-collection-properties.html
http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
Your example worked fine for me. I have included what I did below:
JAVA MODEL
Below is a partial model focusing on the part where you observed the problem.
DataReading
import javax.xml.bind.annotation.*;
#XmlRootElement(name="data_reading")
#XmlAccessorType(XmlAccessType.FIELD)
public class DataReading {
#XmlElement(name="load_survey")
private LoadSurvey load_survey;
}
LoadSurvey
import java.util.List;
import javax.xml.bind.annotation.*;
#XmlAccessorType(XmlAccessType.FIELD)
public class LoadSurvey {
#XmlElement(name="reading")
private List<Reading> readings;
}
Reading
import javax.xml.bind.annotation.*;
#XmlAccessorType(XmlAccessType.FIELD)
public class Reading {
#XmlAttribute
private String interval;
#XmlAttribute
private String value;
}
DEMO CODE
Demo
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(DataReading.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum15833602/input.xml");
DataReading dataReading = (DataReading) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(dataReading, System.out);
}
}
input.xml/Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<data_reading>
<load_survey>
<reading interval="00" value="000.010"/>
<reading interval="01" value="000.000"/>
<reading interval="02" value="000.050"/>
<reading interval="03" value="000.080"/>
<reading interval="04" value="000.010"/>
</load_survey>
</data_reading>

Dozer, how to map from java.util.Map to complex type?

I'd like to map from a java.util.Map to a complex type, let's call it Abc.
<mapping>
<class-a>java.util.Map</class-a>
<class-b bean-factory="xyz.AbcBeanFactory" factory-bean-id="AbcBeanFactory">
xyz.Abc
</class-b>
<field>
<a>Name</a>
<b>companyName</b>
</field>
</mapping>
With that I get this error (which is comprehensible):
org.dozer.MappingException: No read or write method found for field (Name) in class (interface java.util.Map)
Ok, how do I map from a java.util.Map that has an entry with the key 'Name'? Do I have to create a wrapper object that holds that java.util.Map and provide getters/setters for each entry in that map that I want to map?
You can find details for mapping a Map in the Dozer documentation. You need to provide a key not a plain field. Here is an example:
Class Abc:
package com.test;
public class Abc {
private String companyName;
private String companyAddress;
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getCompanyAddress() {
return companyAddress;
}
public void setCompanyAddress(String companyAddress) {
this.companyAddress = companyAddress;
}
}
Mapping file:
<mapping>
<class-a>java.util.Map</class-a>
<class-b>com.test.Abc</class-b>
<field>
<a key="name">this</a>
<b>companyName</b>
</field>
<field>
<a key="address">this</a>
<b>companyAddress</b>
</field>
</mapping>
Test code:
Map<String, String> map = new HashMap<String, String>();
map.put("name", "Company Inc.");
map.put("address", "XYZ Commercial Street");
Abc destObject = dozerMapper.map(map, Abc.class);

Jaxb EclipseLink/MOXy : Is it possible to specify the names of get/set methods

I have a quite simple question :
Say I have a model class defined like this :
public class Test{
private String testAttribute;
public Test(){
}
public String getFormattedTestAttribute(){
return testAttribute + "A nice formatted thingy"; //right, this is just an example
}
public void setTestAttribute(String value){
testAttribute = value;
}
}
You can see that I have a standard setter for testProperty but the getter has a different name : getFormattedTestProperty().
Is it possible into Jaxb/Moxy to specify which getter to use for a specific property ?
I'm using MOXy implementation with external metadata bindings file. The project which I'm working on used tu use Castor. Into Castor's mapping files, you could specify which getter/setter to use like that :
<field name="testAttribute"
get-method="getFormattedTestAttribute">
<bind-xml name="test-attribute" node="attribute"/>
</field>
Is the same kind of thing possible with moxy's external metadata ?
If that kind of customization isn't supported, is it possible to mark a field as read-only and another as write-only ? so I could declare a read-only property named "formattedTestAttribute" and a write-only property named "testAttribute" into the metadata bindings file ?
<!-- read only property -->
<xml-element java-attribute="formattedTestAttribute" xml-path="#test-attribute" />
<!-- write only property -->
<xml-element java-attribute="testAttribute" xml-path="#test-attribute" />
Please note that I have very limited control over the model classes.
Thanks in advance for your answers.
You could represent this in EclipseLink JAXB (MOXy)'s external mapping document as follows:
<?xml version="1.0"?>
<xml-bindings
xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="forum8834871">
<java-types>
<java-type name="Test" xml-accessor-type="PUBLIC_MEMBER">
<xml-root-element/>
<java-attributes>
<xml-element
java-attribute="testAttribute"
name="test-attribute">
<xml-access-methods
get-method="getFormattedTestAttribute"
set-method="setTestAttribute"/>
</xml-element>
<xml-transient java-attribute="formattedTestAttribute"/>
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
Test
I have modified your Test class, to put some logic in the get/set methods.
package forum8834871;
public class Test{
private String testAttribute;
public Test(){
}
public String getFormattedTestAttribute(){
return "APPENDED_ON_GET " + testAttribute;
}
public void setTestAttribute(String value){
testAttribute = "APPENDED_ON_SET " + value;
}
}
Demo
package forum8834871;
import java.io.File;
import java.util.*;
import javax.xml.bind.*;
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, "forum8834871/oxm.xml");
JAXBContext jc = JAXBContext.newInstance(new Class[] {Test.class}, properties);
File xml = new File("src/forum8834871/input.xml");
Unmarshaller unmarshaller = jc.createUnmarshaller();
Test test = (Test) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(test, System.out);
}
}
input.xml
<?xml version="1.0" encoding="UTF-8"?>
<test>
<test-attribute>ORIGINAL</test-attribute>
</test>
Output
<?xml version="1.0" encoding="UTF-8"?>
<test>
<test-attribute>APPENDED_ON_GET APPENDED_ON_SET ORIGINAL</test-attribute>
</test>

JaxB EclipseLink/MOXy : Supposedly empty date marshalled as today's date instead of no writing a node for it

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.
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>

Resources