I am using a Transform
object to save my XML file but it seems to drop empty text nodes. Is there any way to create (and keep) a text node with an empty string
From an XML point of view, there is no difference between <TYPE/>
and <TYPE></TYPE>
. There are both equivalent and can be used interchangeably. For an XML parser it means, that there is no text. The parser doesn't distinguish between "no text" and a "zero length text".
In contrast Java's null
and ""
are totally different concepts.
So if you want to map from Java values to XML and vice versa you have to handle that mismatch. And there are several possible alternatives. For example you can abandon null
values for your String variables. Then you have to ensure, that all your String variables are initialized with empty strings. Or you can say a TYPE element without a text child (serialized as <TYPE/>
or <TYPE></TYPE>
) means the empty string in Java and a missing TYPE element stands for null
. It's your choice.