I've got an XMLGregorianCalendar object that I'm trying to marshal into an xml string. I received this object by unmarshalling another xml object. Both are of type "dateTime", so they should be exactly the same...
And yet, when I marshal it, it shows up blank in the xml.
To illustrate this issue, I stripped everything down to the bare bones and made it generic in this example here. 2 java files, copy, paste, run as-is. The output you should receive would be:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TheObject>
<DOB>2016-09-16</DOB>
</TheObject>
But, alas, it returns:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TheObject>
<DOB></DOB>
</TheObject>
Note: in the pastebin example, I create a xmlGregorianCalendar on the fly rather than grab one from another object like the code below, so technically it's not the same thing, but I think ultimately it illustrates the exact same issue... correct me if I'm wrong...
To add more context to my specific issue:
//Here are the objects themselves (names changed to protect the innocent)
//complete with annotations...
public class Object1{
...
@XmlElement(name = "DOB")
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar dob;
...
}
public class Object2{
...
@XmlElement(name = "DOB")
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar dob;
...
}
//and here's the snippet where the date object(date of birth) gets set
//from one object to another.
object2.setDOB(object1.getDOB());
//and finally, marshalling it to an xml string
private String marshallTheObject(Object2 theObject) throws JAXBException{
JAXBContext jaxbContext = JAXBContext.newInstance(Object2.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter sw = new StringWriter();
jaxbMarshaller.marshal(object2, sw);
String output = sw.toString();
return output;
}
//the xml output shows: <DOB></DOB> instead of the date
I'm using the jaxb version that java 8 comes bundled with...
So my question is: is this some kind of bug? And if not, what am I doing wrong? How can I get around this issue without having to modify the generated java code? I cannot edit the xsd file used to generate it either...
Edit: for reference, the xsd file lists the DOB as:
<xs:element name="DOB" type="xs:dateTime" />