得到它了!我从这个很棒的解决方案改编了代码:
<?php// http://coffeerings.posterous.com/php-simplexml-and-cdataclass 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('<site/>');$xml = new SimpleXMLExtended('<site/>');$xml->title = NULL; // VERY importANT! We need a node where to append$xml->title->addCData('Site Title');$xml->title->addAttribute('lang', 'en');$xml->saveXML($xmlFile);?>
生成的XML文件:
<?xml version="1.0"?><site> <title lang="en"><![CDATA[Site Title]]></title></site>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)