So I was trying to set an empty attribute when an element has no value.
So my class looks like this:
@Order(attributes = { "name" })
public class TypeNumber {
@Attribute(required = false)
protected String name;
@Element(required = false)
@Attribute(empty = "xsi:nil=\"true\"")
protected BigDecimal value;
//getter setter methods goes here
}
In case of empty value I was expecting output:
<field name="some_name">
<value xsi:nil="true"/>
</field>
While the actual output is:
<field name="some_name"/>
Any idea why the empty attribute is not working as expected? Or am I doing it wrong ?
Note: I am using SimpleFramework XML with VisitorStrategy
. So, can't use AnnotationStrategy
. Also I have custom Visitor to read and write nodes.
You will need a custom Converter...
Right now your outputs are:
<typeNumber name="original" value="10"/>
and
<typeNumber name="original_empty" value="xsi:nil="true""/>
So let's get started!
First thing you got to do is pass AnnotationStrategy to the constructor of your Persister:
Serializer serializer = new Persister(new AnnotationStrategy());
.. then create a custom Converter in your model:
This will generate following output for empty/nonempty values respectively:
typeNumberFixed.value = null:
typeNumberFixed.value = 30:
Here's the repo if you're interested.