views:

3042

answers:

3

Hello,

This is a pretty simple request, but I just didn't find a way to do it.

I'm basically trying to set up a role in JAXB which says that whenever an null field is encountered, instead of ignoring it in the output, set it to an empty value. So for the class :

@XMLRootElement
Class Foo {
   Integer num;
   Date date;
….
}

When this has been marshalled into the XML file if the date field is null, my output does not have that element in it. What I want to do is include all the fields in the output; and if they are null, replace them with - say a blank. So the output should be :

<foo>
  <num>123</num>
  <date></date>
</foo>

Thanks,

Jalpesh.

A: 

But but but...a empty string is not a valid lexical representation for a date, so you can't do that. i.e., if you generated an XML document with an empty value for a date field, it won't validate properly.

In other words, if your date element has a minOccurs of 1 or more and not nillable, then you absolutely must have (1 or more) dates, which can't be null (or blanks, or other non-values).

Chris Jester-Young
I used to have the first paragraph of this answer as a comment (thanks to the lovely person who upvoted it!), but I figured it'd be more useful as an answer, now that I've expanded on it a bit. :-)
Chris Jester-Young
Agreed, but i'm not validating the xml, and it doesn't have a schema. Having all the fields present will make it easier for the end consumer to figure out what this xml means.Before you start berating me for not having an xml schema :) I'm using this from within another framework (Jersey) which consumes my data objects and spits out XML/Json using JAXB. So the only control I have is over my data objects.In addition I can see a reason to support null values. Think of a use case where I want to generate a CSV file out of my data object. I could use marshal.marshal(elem, myContentHandler).
Jalpesh
The contentHandler would then generate CSV file with the right format, which can only be done if none of my data fields are ignored.
Jalpesh
A: 

As indicated in the other answer is invalid since it is not a valid date. I had a similar issue where I wanted to handle (same as ) specially. Since you cannot use null, you can use the default value mechanism in JAXB. The following will default the value if none is specified. You can through code detect this special date and handle this exception case.

@XmlElement(defaultValue="1970-01-01T00:00:00.0-00:00")

So it is possible to detected and empty date value but you just cannot use null to do it.

Chris Dail
+3  A: 

Thanks guys for your answers.

Chris Dail - I tried your approach, and it didn't really do what I wanted. JAXB was still ignoring my null values, inspite of defining a default value for my fields.

I did stumble across the answer after somebody in jersey forums pointed me to a link :

https://jaxb.dev.java.net/tutorial/section_2_2_12_8-No-Value.html#No%20Value

Basically, all I had to do was to add the following to my fields :

@XmlElement(nillable = true)

Once I added that, JAXB would show up hose fields when marshalling them to XML like this :

...
<num>5</num>
<date xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/&gt;
....
Jalpesh