无法在XML节点中创建空值 [英] Cannot create empty value in XML node
问题描述
我正在尝试将子元素附加到XML节点
I am trying to append a child element to XML node
$rel->appendChild($domtree->createElement('title',NULL));
我希望它以这种方式输出
I want it to output like this
<title></title>
但是我得到了这个
<title/>
如何使用空值创建它?
推荐答案
您需要显式添加一个空文本节点:
You need to explicitly add an empty text node:
$title = $domtree->createElement('title');
$title->appendChild($domtree->createTextNode(''));
$rel->appendChild($title);
createElement()
的第二个参数是非标准的,我个人不使用它,因为它可能会产生类似这样的不直观行为。
The second argument to createElement()
is non-standard and I personally don't use it, because it can produce slightly unintuitive behaviour like this.
在我看来,您应该始终显式创建文本节点。这样做的另一个原因是,文本节点会自动正确地进行转义,这与非标准第二个参数不同,后者要求您手动转义文本数据。
You should always create text nodes explicitly in my opinion. Another reason for doing this is that text nodes automatically handle escaping correctly, unlike the non-standard second argument which requires that you manually escape the text data.
这篇关于无法在XML节点中创建空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!