I have XML file like:
<?xml version="1.0" encoding="UTF-8"?>
<INFO version="2.0">
<Ad id="602867">
<Wrapper>
<AdSystem><![CDATA[Acudeo Compatibl]]e</AdSystem>
<INFOTagURI>
<![CDATA[url]]>
</INFOTagURI>
<Impression><![CDATA[old_url]]></Impression>
</Wrapper>
</Ad>
</INFO>
I want to change old_url
CDATA value to new_url
I tried code like this:
$domDocument = new \DOMDocument('1.0', 'UTF-8');
$domDocument->loadXML($xml);
$impressionNode = $domDocument->getElementsByTagName("Wrapper")->item(0)->getElementsByTagName('Impression')->item(0);
$parentNode = $impressionNode->parentNode;
$oldNode = $impressionNode;
$newNode = $parentNode->appendChild($domDocument->createElement('Impression'));
$cdata = $domDocument->createCDATASection('new_url');
$newNode->appendChild($cdata);
$parentNode->removeChild($oldNode);
$parentNode->appendChild($newNode);
echo $domDocument->saveXML();
And it still returning old_url
but debugging this logic I can see that $parentNode
value changing to new_url
, so looks like it require some logic to update parent of the parent and so to the root element??
FYI: Also, I tried to use replaceChild
.
Please help me to solve this problem, thank you!