XStream parser empty tag for null values

2k views Asked by At

I'm using XStream parser. I wants to get an empty tag in place of null values for a variable. How do I achieve this?

Example:

class Person{
    private String name;
    private String age;
}

Person person = new Person("Joe", null);

I'm getting this,

<Person>
    <name>Joe</name>
</Person>

I need this,

<Person>
    <name>Joe</name>
    <age></age>
</Person>
1

There are 1 answers

3
VinKel On

If it only concerns strings, initialise them with "", not with null.

So instead of:

Person person = new Person("Joe", null);

Try:

Person person = new Person("Joe", "");

Also, make sure to take a look at: XStream serialize null values