Seen a couple of bug reports and gotchas with this one but not a simple code example. Just in case someone finds it useful:
<?php
header('Content-type: application/xml');
$xml = new XmlWriter();
$xml->openMemory();
$xml->startDocument('1.0', 'UTF-8');
$xml->startElement('mydoc');
$xml->startElement('myele');
$xml->startElement('mycdataelement');
$xml->writeCData("text for inclusion within CData tags");
$xml->endElement();
$xml->endElement();
$xml->endElement();
echo $xml->outputMemory(true);
?>
Outputs:
<?xml version="1.0" encoding="UTF-8"?>
<mydoc>
<myele>
<mycdataelement><![CDATA[text for inclusion within CData tags]]>
</mycdataelement>
</myele>
</mydoc>