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>
But I got this instead
<title/>
How to create this with an empty value ?
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>
But I got this instead
<title/>
How to create this with an empty value ?
You need to explicitly add an empty text node:
$title = $domtree->createElement('title');
$title->appendChild($domtree->createTextNode(''));
$rel->appendChild($title);
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.