I am creating copy of xml file and try to change local.xml file database name dynamically but CData is not getting proper.

my code is below,

 $xml = new DOMDocument('1.0', 'utf-8');
 $xml->formatOutput = true; 
 $xml->preserveWhiteSpace = false;
 $xpath = new DOMXpath($xml);
 $element = $xml->getElementsByTagName('dbname')[0];  
 $element->nodeValue = '<![CDATA['test']]>'; 

But output is <dbname>&lt;![CDATA[test instance]]&gt;</dbname>.

I need output with <dbname><![CDATA[test instance]]></dbname>.

Can any one have solution how i can achieve this.

closed as off-topic by sv3n, Manoj Deswal, Dhiren Vasoya, Priyank, Siarhey Uchukhlebau Apr 25 '18 at 5:39

  • This question does not appear to be about the Magento e-Commerce platform within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • You can try createCDATASection('test'). – sv3n Apr 24 '18 at 10:39
  • can you please give me example or link ? – denish vachhani Apr 24 '18 at 10:56
  • 4
    I'm voting to close this question as off-topic because it is not really related to Magento. – sv3n Apr 24 '18 at 18:23
  • Added example ...you can also try replaceChild() - see php.net/manual/de/domnode.replacechild.php – sv3n Apr 25 '18 at 10:47
  • Thx for feedback :( – sv3n Oct 11 '18 at 23:55

You can try this - it clears current value and adds your CDATA:

$element = $xml->getElementsByTagName('dbname')->item(0);
$element->nodeValue = '';

Not the answer you're looking for? Browse other questions tagged or ask your own question.