Java 在 DOM 文档中创建空文本节点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22522558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create empty text node in DOM Document
提问by Indigo
How can we create an empty node in a DOM document without getting java.lang.NullPointerException
while writing XML files with Transformer
?
我们如何在 DOM 文档中创建一个空节点而不会java.lang.NullPointerException
在使用Transformer
.
I am writing an XML Document with some nodes possibly with empty values using element.appendChild(documnet.createTextNode(nodeValue));
我正在编写一个 XML 文档,其中一些节点可能使用空值 element.appendChild(documnet.createTextNode(nodeValue));
Here this nodeValue could be an empty String or lets say null or empty value, then how could we force it to write this empty text node like <emptyTextNode/> or <emptyTextNode><emptyTextNode>
这里这个 nodeValue 可能是一个空字符串或者让我们说 null 或空值,那么我们怎么能强迫它写这个空文本节点,比如 <emptyTextNode/> or <emptyTextNode><emptyTextNode>
If I just simply write this node with an empty value, Transformer
throws NullPointerException
, while transforming the Document to XML.
如果我只是简单地用一个空值编写这个节点,Transformer
throws NullPointerException
,同时将 Document 转换为 XML。
This nullpointer exception needs to be avoided and allow this empty node in output XML anyhow condition.
需要避免此空指针异常,并在输出 XML 条件下允许此空节点。
采纳答案by helderdarocha
If you are using Java SE org.w3c.dom you will only get NullPointerException if you append a null
child. If you append no childor an empty text nodeyou will get an empty element, but not a NullPointerException
.
如果您使用的是 Java SE org.w3c.dom,则只有在附加null
子项时才会得到 NullPointerException 。如果您不附加任何子节点或空文本节点,您将获得一个空元素,而不是一个NullPointerException
.
For example, suppose you create these elements in a DOM:
例如,假设您在 DOM 中创建这些元素:
Element root = doc.createElement("root");
Element textNode = doc.createElement("textNode");
Element emptyTextNode = doc.createElement("emptyTextNode");
Element emptyNode = doc.createElement("emptyNode");
Element nullNode = doc.createElement("nullTextNode");
textNode.appendChild(doc.createTextNode("not empty")); // <textNode>not empty</textNode>
emptyTextNode.appendChild(doc.createTextNode("")); // <emptyTextNode></emptyTextNode>
// emptyNode: no child appended <emptyNode />
nullNode.appendChild(null); // null child appended - causes NPE!!
root.appendChild(textNode);
root.appendChild(emptyTextNode);
root.appendChild(emptyNode);
root.appendChild(nullNode);
Only the nullNode
causes NullPointerException
. To avoid it, you should guarantee that nodeValue
is not null, but it can be an empty string.
只有nullNode
原因NullPointerException
。为了避免它,你应该保证它nodeValue
不是空的,但它可以是一个空字符串。
If the line that causes NPE is commented, the result will be:
如果注释了导致 NPE 的行,结果将是:
<root>
<textNode>not empty</textNode>
<emptyTextNode/>
<emptyNode/>
<nullTextNode/>
</root>
回答by Indigo
Well, I found my mistake here,
好吧,我在这里发现了我的错误,
I am using element.appendChild(documnet.createTextNode(nodeValue));
to write the empty or null value to node. Which is not correct.
我正在使用element.appendChild(documnet.createTextNode(nodeValue));
将空值或空值写入节点。这是不正确的。
I should simply just create the Element and leave it like this without attempting to write null as TextNode value. Since empty or null can never be a TextNode.
我应该简单地创建 Element 并保持原样,而不要尝试将 null 作为 TextNode 值写入。因为 empty 或 null 永远不能是 TextNode。
So, I have to be extra careful in my code and avoid trying to write null values. In other words, check the value first, if it's not null then only write as TextNode otherwise simply write the Element and leave it to be interpreted by the serializer while transforming.
所以,我必须在我的代码中格外小心,避免尝试编写空值。换句话说,首先检查值,如果它不为空,则只写为 TextNode 否则只需写入 Element 并在转换时由序列化程序解释它。
Element childElem = document.createElement("Child");
rootElement.appendChild(childElem);
Now if some String is not null then only,
现在,如果某些 String 不为空,则仅,
childElem.appendChild(document.createTextNode(someString));