3

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
  • Try to set empty string. Commented Jan 5, 2017 at 15:23

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

3 Comments

If its two fields then its ok. I have 12 classes which has around 30 fields in each class aprox. I can't set to each variable. Is there any configuration for that whichever variable is null it should set an empty string like that?
Corry, couldn't really find/think of any other way... Maybe this Correct way to represent null XML elements or this XML serialization, hide null values might help provide any help?
Actually, what Saulius Next commented on your question seems to work, but that's the same solution as I have provided, but which looks neater. How about providing such default values in the getters and setters? Could you extend a bit on what your situation is?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.