note 128142 added to simplexmlelement.addchild

From: Date: Mon, 23 Jan 2023 11:18:46 +0000
Subject: note 128142 added to simplexmlelement.addchild
Groups: php.notes 
Request: Send a blank email to php-notes+get-220098@lists.php.net to get a copy of this message
The class below allows you to write CDATA and to add additional attributes. <?php class SimpleXMLElementExtended extends \SimpleXMLElement { public function addChildWithCData($name, $value) { $child = parent::addChild($name); $element = dom_import_simplexml($child); $docOwner = $element->ownerDocument; $element->appendChild($docOwner->createCDATASection($value)); return $child; } } ?> Example: <?php $xml = new SimpleXMLElementExtended('<xml></xml>'); $content = $xml->addChildWithCData('content', 'Title of the page'); $content->addAttribute('id', 1); $content->addAttribute('enabled', 'true'); // Output: // <xml> // <content id="1" enabled="true"><![CDATA[Title of the page]]></content> // </xml> ?> ---- Server IP: 45.112.84.4 Probable Submitter: 45.91.158.213 (proxied: 94.110.184.89) ---- Manual Page -- https://php.net/manual/en/simplexmlelement.addchild.php Edit -- https://main.php.net/note/edit/128142 Del: integrated -- https://main.php.net/note/delete/128142/integrated Del: useless -- https://main.php.net/note/delete/128142/useless Del: bad code -- https://main.php.net/note/delete/128142/bad+code Del: spam -- https://main.php.net/note/delete/128142/spam Del: non-english -- https://main.php.net/note/delete/128142/non-english Del: in docs -- https://main.php.net/note/delete/128142/in+docs Del: other reasons-- https://main.php.net/note/delete/128142 Reject -- https://main.php.net/note/reject/128142 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#220098) next »