How to create an XML text node with an empty string value (in Java)

后端 未结 7 1651
温柔的废话
温柔的废话 2020-12-16 16:00

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

相关标签:
7条回答
  • 2020-12-16 17:02

    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.

    0 讨论(0)
提交回复
热议问题