Skip to content

Core\Xml\Writer::writeText

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

Writes text inside the open element, such as the name of a product or the body of a note.

The writer escapes the text for you. & becomes &amp;, < becomes &lt; and > becomes &gt;. A parser reads these back as the original characters, so it gets back exactly the text you wrote. This also means that a text can never add an element or an attribute to your document. It is safe to write text that a user typed.

Text must be inside an element, so writeText throws a LogicError when no element is open. XML cannot contain most control characters, such as the character with code 0. A text that contains one also throws a LogicError, and nothing is written. After you write text into an element, the writer does not indent inside that element, because added whitespace would change the text.

The examples below show the escaped output, the two errors, and an export of a product list.

Parameters

  • $textstringThe text to write. It can contain any character XML allows, including <, & and quotes. A tainted text is allowed, because the writer escapes it.

Return value

Returns void. Nothing. After this call, the writer does not indent inside this element, because added whitespace would change the text.

Errors

  • LogicErrorNo element is open. 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

Special characters are escaped

<?nvs
// Shows how `writeText` escapes a text, and how a parser reads the same text back.
string $text = "Fish & Chips <today only> for \"2 people\"";
Core\Xml\Writer $writer = Core\Xml::writer();
$writer->startDocument();
$writer->startElement('offer');
// `&`, `<` and `>` are written as `&amp;`, `&lt;` and `&gt;`. Quotes are written unchanged.
$writer->writeText($text);
$writer->endElement();
string $document = $writer->endDocument();
echo $document, "\n";
// The parser gives back the original text. This prints the text, and then `true`.
tainted string $read = Core\Xml::parse($document)->children()[0]->children()[0]->text();
echo $read, "\n";
echo $read == $text ? 'true' : 'false', "\n";
Output
<?xml version="1.0" encoding="UTF-8"?><offer>Fish &amp; Chips &lt;today only&gt; for "2 people"</offer>
Fish & Chips <today only> for "2 people"
true

Texts that are not allowed

<?nvs
// Shows the two errors `writeText` throws, and that the document is still correct afterwards.
Core\Xml\Writer $writer = Core\Xml::writer();
$writer->startDocument();
// No element is open yet. This prints the error message.
try {
$writer->writeText('Hello');
} catch (LogicError $error) {
echo $error->message, "\n";
}
$writer->startElement('note');
// The character with code 0 cannot be written in XML. This prints the error message.
try {
$writer->writeText("Hello\x00World");
} catch (LogicError $error) {
echo $error->message, "\n";
}
// A tab and a line break are allowed.
$writer->writeText("Hello\tWorld\n");
$writer->endElement();
echo $writer->endDocument(), "\n";
Output
Core\Xml\Writer::writeText(): character data belongs inside an element, and none is open.
Core\Xml\Writer::writeText(): the text holds U+0000, which a document has no way to write.
<?xml version="1.0" encoding="UTF-8"?><note>Hello World
</note>

Export a product list

<?nvs
// Shows how to export a product list as XML, for example for a price comparison website.
array<string> $products = [
'A-100' => 'Salt & Pepper Set',
'B-200' => 'Cable <USB-C> 2m',
'C-300' => 'Café Crème Cups',
];
Core\Xml\Writer $writer = Core\Xml::writer({ indent: ' ' });
$writer->startDocument();
$writer->startElement('products');
foreach ($products as string $sku => string $name) {
$writer->startElement('product');
$writer->writeAttribute('sku', $sku);
// The name is escaped, so a name with `&` or `<` does not break the file.
$writer->writeText($name);
$writer->endElement();
}
$writer->endElement();
echo $writer->endDocument(), "\n";
Output
<?xml version="1.0" encoding="UTF-8"?>
<products>
<product sku="A-100">Salt &amp; Pepper Set</product>
<product sku="B-200">Cable &lt;USB-C&gt; 2m</product>
<product sku="C-300">Café Crème Cups</product>
</products>