PHP 8.5.0 Alpha 4 available for testing

Voting

: eight plus one?
(Example: nine)

The Note You're Voting On

fluxlicious at gmail dot com
2 years ago
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>
?>

<< Back to user notes page

To Top