I'm building an XML like:
<?xml ... ?>
<root>
<elements>0</elements>
<list>
<element>test1</element>
<element>test1</element>
<element>test1</element>
</list>
</root>
After appending all <element>
s, I want to replace <elements>0</elements>
by <elements>3</elements>
for example.
I tried DOMNode::replaceChild
, but it has no affect.
$numberOfElements = $xml->createElement('numberOfElements', '0');
$root->appendChild($numberOfElements);
/* append elements and count them */
$root->replaceChild($numberOfElements,
$xml->createElement('numberOfElements', $countElements)
);
How to properly use replaceChild or is there a different way?
From the docs:
public DOMNode replaceChild ( DOMNode $newnode , DOMNode $oldnode )
This means that you must specify the new node first, then the node to be replaced. You have it the wrong way around.
EDIT: That said, why not do this?
$numberOfElements->nodeValue = $countElements;