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

后端 未结 7 1649
温柔的废话
温柔的废话 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 16:45

    A text node without text is not a text node.

    If you are trying to control how the XML element is serialized, and are equivalent, and it will not matter to an XML processor if either was used. Both are declaring a TYPE element without any text(). Most processors will serialize an empty element as a self-closing element.

    If you really want to prevent the element from being serialized as self-closing, you could get cute and add a zero-width space as the text node value: which will look like: .

    It isn't technically an "empty" string, but might achieve what you want, and will not pad with space if the text node is selected and used.

提交回复
热议问题