如何使用空字符串值创建XML文本节点(在Java中) [英] How to create an XML text node with an empty string value (in Java)

查看:61
本文介绍了如何使用空字符串值创建XML文本节点(在Java中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Transform 对象保存我的XML文件,但是它似乎删除了空的文本节点.有什么方法可以创建(并保留)带有空字符串(即")的文本节点.

这是我创建节点的方式:

 元素类型= doc.createElement("TYPE");type.appendChild(doc.createTextNode(value)); 

只是有时 value 是一个空字符串".当我使用文本编辑器查看XML时,会看到

 < TYPE/> 

代替

 < TYPE></TYPE> 

我重新读完这个XML文件并遍历节点后,< TYPE>即使我为它显式创建了一个#text节点,该元素也根本没有任何子元素!

编辑-感恩节快乐(对于我的加拿大同胞)

重要说明,我不是使用一组已知的标记进行工作,而是我正在使用的程序使用文本节点的存在将 JTextField 分配给GUI.只是有时字段保留为空(不是null而是").当我存储该字段然后将其读回时,GUI不再呈现 JTextField ,因为没有文本节点.因此,我正在研究创建XML文档的方法,即使该文档中没有任何内容,该XML文档也会创建一个文本节点.如果用XML根本无法做到这一点,那么我将不得不使用一个属性来标记可编辑的标签(有点像Andrey Breslav建议的那样),因此应该分配一个 JTextField .

我将尝试Mads Hansen关于不间断空间的建议.

解决方案

没有文本的文本节点 不是文本节点 ./p>

如果您尝试控制XML元素的序列化方式,则< TYPE/> < TYPE></TYPE> 是等效的,并且使用XML处理器与XML处理器无关紧要.两者都声明没有任何 text()的TYPE元素.大多数处理器会将空元素序列化为自闭元素.

如果您确实想防止元素被序列化为自闭合,则可能会变得可爱,并添加零宽度的空格作为文本节点值: < TYPE>&#x200B;</TYPE> 如下所示:< TYPE></TYPE> .

从技术上讲,它不是空"字符串,但是可以实现您想要的,并且如果选择并使用了文本节点,则不会用空格填充.

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 i.e. "".

Here is how I create the node:

Element type = doc.createElement("TYPE");

type.appendChild(doc.createTextNode(value));

It is just that sometimes value is an empty string "". When I look at the XML with a text editor I see

<TYPE />

instead of

<TYPE></TYPE>

After I read this XML file back in and traverse the nodes the <TYPE> element simply doesn't have any children even though I explicitly created one #text node for it!

EDIT - Happy Thanksgiving (for my fellow Canadians)

Important note, I am not working from a known set of tags, rather the program I am working on uses the presence of a text node to assign a JTextField to the GUI. It is just that sometimes the field is left empty (not null but ""). When I store that field and then read it back the GUI doesn't render the JTextField anymore because there is no text node. So I am looking at ways to create an XML document that creates a text node even if there is nothing in it. If that simply can't be done with XML then I will have to use an attribute to mark tags that are editable (somewhat like Andrey Breslav suggested) and so should have a JTextField assigned.

I will try Mads Hansen's suggestion of a non-breaking space.

解决方案

A text node without text is not a text node.

If you are trying to control how the XML element is serialized, <TYPE/> and <TYPE></TYPE> 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: <TYPE>&#x200B;</TYPE> which will look like: <TYPE></TYPE>.

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.

这篇关于如何使用空字符串值创建XML文本节点(在Java中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆