Core\Xml\Writer::writeComment
Writes an XML comment, such as <!-- Generated file -->, at the place the writer has reached.
You can write a comment before the root element, inside an element, or after the root element is closed. A program that reads the document skips comments, so they are notes for a person.
XML has no way to escape anything inside a comment. The writer writes your text as it is. For this reason, the text cannot contain two dashes in a row (--), and it cannot end with a dash. If it does, writeComment throws a LogicError and writes nothing. A text with < or & is allowed, and it is written unchanged.
The examples below add comments in three places, show the two errors, and put a header comment at the top of an exported file.
Parameters
$textstringThe comment's text. It is written unchanged, because XML cannot escape anything inside a comment. It cannot contain--, and it cannot end with-.
Return value
Returns void. Nothing. The comment is written at the place the writer has reached.
Errors
LogicErrorThe text contains--or ends with-. The text contains a control character that XML cannot write. The document is not open.
Changelog
| Version | Change |
|---|---|
0.0.6 | Every 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
Comments in three places
<?nvs// Shows how to write comments before the root element, inside it and after it.
Core\Xml\Writer $writer = Core\Xml::writer({ indent: ' ' });$writer->startDocument();
// This comment is written before the root element.$writer->writeComment(' Settings for the shop ');$writer->startElement('settings');
// This comment is written inside `settings`, on its own line.$writer->writeComment(' Prices are in euro ');$writer->startElement('currency');$writer->writeText('EUR');$writer->endElement();$writer->endElement();
// This comment is written after the root element is closed.$writer->writeComment(' End of settings ');echo $writer->endDocument(), "\n";<?xml version="1.0" encoding="UTF-8"?><!-- Settings for the shop --><settings> <!-- Prices are in euro --> <currency>EUR</currency></settings><!-- End of settings -->Dashes that are not allowed
<?nvs// Shows which comment texts throw an error, and how to write the same note another way.
Core\Xml\Writer $writer = Core\Xml::writer();$writer->startDocument();$writer->startElement('log');
// Two dashes in a row are not allowed in a comment. This prints the error message.try { $writer->writeComment('step 1 -- step 2');} catch (LogicError $error) { echo $error->message, "\n";}
// A comment cannot end with a dash either. This throws the same error.try { $writer->writeComment('to do: -');} catch (LogicError $error) { echo $error->message, "\n";}
// One dash between other characters is allowed.$writer->writeComment('step 1 - step 2');
$writer->endElement();echo $writer->endDocument(), "\n";Core\Xml\Writer::writeComment(): a comment has no escape grammar, so it cannot hold `--` or end with `-`.Core\Xml\Writer::writeComment(): a comment has no escape grammar, so it cannot hold `--` or end with `-`.<?xml version="1.0" encoding="UTF-8"?><log><!--step 1 - step 2--></log>Mark an export as generated
<?nvs// Shows how to put a note at the top of an exported file, so nobody edits it by hand.
array<string> $pages = ['/', '/tea', '/contact'];
Core\Xml\Writer $writer = Core\Xml::writer({ indent: ' ' });$writer->startDocument();$writer->writeComment(' This file is generated. Changes made by hand are lost. ');$writer->startElement('urlset');
foreach ($pages as string $page) { $writer->startElement('url'); $writer->startElement('loc'); $writer->writeText('https://example.com' . $page); $writer->endElement(); $writer->endElement();}
$writer->endElement();echo $writer->endDocument(), "\n";<?xml version="1.0" encoding="UTF-8"?><!-- This file is generated. Changes made by hand are lost. --><urlset> <url> <loc>https://example.com/</loc> </url> <url> <loc>https://example.com/tea</loc> </url> <url> <loc>https://example.com/contact</loc> </url></urlset>