Add data in CDATA format in phpsimpleXML

Source: Internet
Author: User
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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.