When I marshall a java object using JAXB Marshaller, the marshaller does not create empty elements for null files in the java object. For example, I have a following java object:
public class PersonTraining {
@XmlElement(name = "Val1", required = true)
protected BigDecimal val1;
@XmlElement(name = "Val2", required = true, nillable = true)
protected BigDecimal val2;
@XmlElement(name = "Val3", required = true, nillable = true)
@XmlSchemaType(name = "dateTime")
protected XMLGregorianCalendar val3;
}
When I take an instance of this object, and marshall into an XML, I get the following (This is beacuse I did not set the value for Val2):
<PersonTraining>
<Val1>1</Val1>
<Val3>2010-01-01T00:00:00.0-05:00</Val3>
</PersonTraining>
However, I had expected hte following result from the marshalling operation (Infact, I specifically need element as well so that the XML can be validated against the XSD)
<PersonTraining>
<Val1>1</Val1>
<Val2></Val2>
<Val3>2010-01-01T00:00:00.0-05:00</Val3>
</PersonTraining>
Please let me know what option I would need to set so that the null value in the object attributes can ALSO be marshalled, and returned as empty/null elements.
Here is the marshalling code:
StringWriter sw = new StringWriter();
JAXBContext jc = JAXBContext.newInstance("person_training");
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(ptl, sw);