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');
?>