I have this code to create and update xml file:
<?php
$xmlFile = 'config.xml';
$xml = new SimpleXmlElement('<site/>');
$xml->title = 'Site Title';
$xml->title->addAttribute('lang', 'en');
$xml->saveXML($xmlFile);
?>
This generates the following xml file:
<?xml version="1.0"?>
<site>
<title lang="en">Site Title</title>
</site>
The question is: is there a way to add CDATA with this method/technique to create xml code below?
<?xml version="1.0"?>
<site>
<title lang="en"><![CDATA[Site Title]]></title>
</site>
Here's my version of this class that has a quick addChildWithCDATA method, based on your answer:
Simply use it like that:
Got it! I adapted the code from this great solution:
XML file generated:
Thank you Petah
You can also create a helper function for this, if you'd rather not extend SimpleXMLElement:
Here is my combined solution with adding child with CDATA or adding CDATA to the node.