We know that simpleXML in php cannot directly and conveniently add data in CDATA format, which may cause some problems during our operations, next I will introduce you to phpsimpleXML to add a CDATA format data method, p... we know that simpleXML in php cannot directly and conveniently add data in CDATA format, which may cause some problems during our operations, next, I will introduce you to a method for adding CDATA format data in php simpleXML. The php instance code is as follows:
Site Title instead of Site Title * */ class SimpleXMLExtended extends SimpleXMLElement { public function addCData($cdata_text) { $node = dom_import_simplexml($this); $no = $node->ownerDocument; $node->appendChild($no->createCDATASection($cdata_text)); } }$xmlFile = 'config.xml'; // instead of $xml = new SimpleXMLElement('
'); $xml = new SimpleXMLExtended('
'); $site = $xml->addChild('site'); // instead of $site->addChild('site', 'Site Title'); $site->title = NULL; // VERY IMPORTANT! We need a node where to append $site->title->addCData('Site Title'); $site->title->addAttribute('lang', 'en'); $xml->asXML($xmlFile);