Skip to content

Core\Xml\Writer::writeCdata

$writer->writeCdata(string $text): void$writer->writeCdata(
    string $text,
): void

Writes text inside the open element as a CDATA section, such as <![CDATA[a < b]]>.

Inside a CDATA section, < and & are ordinary characters. The writer writes your text unchanged, and it does not escape anything. This is useful for text that contains a lot of markup or code, such as HTML in a news feed. A parser reads a CDATA section as normal text, so it gets back the same text that writeText would give.

A CDATA section ends at ]]>, so the text cannot contain ]]>. If it does, writeCdata throws a LogicError and writes nothing. Use writeText for such a text. A CDATA section must be inside an element, so writeCdata also throws a LogicError when no element is open.

The examples below compare writeCdata with writeText, show the two errors, and put HTML into a news feed.

Parameters

  • $textstringThe text to write. < and & are allowed and are not escaped. The text cannot contain ]]>, because that ends a CDATA section.

Return value

Returns void. Nothing. After this call, the writer does not indent inside this element, like after writeText.

Errors

  • LogicErrorNo element is open. The text contains ]]>. The text contains a control character that XML cannot write. The document is not open.

Changelog

VersionChange
0.0.6Every Core\Xml\Writer method that writes a part of the document now starts with write. For example, writeText writes text and writeAttribute writes an attribute. The old names were content, attribute, comment, cdata, instruction and doctype.

Examples

Cdata and content

<?nvs
// Shows how `writeCdata` writes a text unchanged, and how `writeText` escapes the same text.
string $code = 'if (a < b && b < c) { ok(); }';
Core\Xml\Writer $writer = Core\Xml::writer();
$writer->startDocument();
$writer->startElement('snippets');
// `writeText` writes `<` as `&lt;` and `&` as `&amp;`.
$writer->startElement('escaped');
$writer->writeText($code);
$writer->endElement();
// `writeCdata` writes the text unchanged, inside `<![CDATA[` and `]]>`.
$writer->startElement('unchanged');
$writer->writeCdata($code);
$writer->endElement();
$writer->endElement();
string $document = $writer->endDocument();
echo $document, "\n";
// A parser reads both elements as the same text. This prints the code, and then `true`.
Core\Xml\Node $root = Core\Xml::parse($document)->children()[0];
tainted string $escaped = $root->children()[0]->children()[0]->text();
tainted string $unchanged = $root->children()[1]->children()[0]->text();
echo $unchanged, "\n";
echo $escaped == $unchanged ? 'true' : 'false', "\n";
Output
<?xml version="1.0" encoding="UTF-8"?><snippets><escaped>if (a &lt; b &amp;&amp; b &lt; c) { ok(); }</escaped><unchanged><![CDATA[if (a < b && b < c) { ok(); }]]></unchanged></snippets>
if (a < b && b < c) { ok(); }
true

Texts that are not allowed

<?nvs
// Shows the two errors `writeCdata` throws, and how to write such a text with `writeText`.
Core\Xml\Writer $writer = Core\Xml::writer();
$writer->startDocument();
// No element is open yet. This prints the error message.
try {
$writer->writeCdata('Hello');
} catch (LogicError $error) {
echo $error->message, "\n";
}
$writer->startElement('note');
// `]]>` ends a CDATA section, so it is not allowed in the text. This prints the error message.
string $text = 'The end marker is ]]> in XML.';
try {
$writer->writeCdata($text);
} catch (LogicError $error) {
echo $error->message, "\n";
}
// `writeText` escapes `>`, so it can write the same text.
$writer->writeText($text);
$writer->endElement();
echo $writer->endDocument(), "\n";
Output
Core\Xml\Writer::writeCdata(): character data belongs inside an element, and none is open.
Core\Xml\Writer::writeCdata(): a CDATA section has no escape grammar, so it cannot hold `]]>`.
<?xml version="1.0" encoding="UTF-8"?><note>The end marker is ]]&gt; in XML.</note>

Html in a news feed

<?nvs
// Shows how to put HTML into a news feed, so a feed reader can show it with its formatting.
array<string> $posts = [
'New teas in stock' => '<p>Three new <b>green teas</b> are now in the shop.</p>',
'Summer sale' => '<p>Everything is 10% off &amp; shipping is free.</p>',
];
Core\Xml\Writer $writer = Core\Xml::writer({ indent: ' ' });
$writer->startDocument();
$writer->startElement('rss');
$writer->writeAttribute('version', '2.0');
$writer->startElement('channel');
foreach ($posts as string $title => string $html) {
$writer->startElement('item');
$writer->startElement('title');
$writer->writeText($title);
$writer->endElement();
// The HTML is written unchanged, so the feed stays easy to read.
$writer->startElement('description');
$writer->writeCdata($html);
$writer->endElement();
$writer->endElement();
}
$writer->endElement();
$writer->endElement();
echo $writer->endDocument(), "\n";
Output
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<item>
<title>New teas in stock</title>
<description><![CDATA[<p>Three new <b>green teas</b> are now in the shop.</p>]]></description>
</item>
<item>
<title>Summer sale</title>
<description><![CDATA[<p>Everything is 10% off &amp; shipping is free.</p>]]></description>
</item>
</channel>
</rss>