JAXB Java to XML: How not printing primitive type members when default
-
22-07-2019 - |
Question
First attempt to use this cool site - after searching for 2 hours: So I have a Java Bean that is given (I can only annotate not change) and need to map it to XML using JAXB. I would like primitives types not to be printed when they contain their language default, or a user-defined default.
As said I cannot change the java bean, and therefore change the primitive types into their Object Wrappers counterparts.
How do you do that best?
Sample bean:
class Foo {
public String name;
// -1 is user defined default, to indicate field is not set.
public long someIdx=-1;
// ...
}
Foo f = new Foo();
f.name = "Duke";
for this instantiation, what I would like is the following output:
<foo><name>Duke</name></foo>
Solution
You could use JAXB's XmlAdapters. Availible since JAXB 2.0 (JDK 6.0)
Using the adapter, you let the bean alone and annotate/change a completely different class. This should be not a problem in your case.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow