Core\Xml\Writer::endElement
$writer->endElement(): voidCloses the element that startElement opened last. You do not pass the element's name, because the writer remembers which elements are open and in which order. So every end tag matches its start tag, and a document can never close <a> with </b>.
An element with nothing inside is written in the short form <a/>. An element with text or other elements inside is closed with </a>.
When no element is open, endElement throws a LogicError. If you forget to close an element, endDocument throws a LogicError that names the element that is still open.
The examples below show the two forms of an end tag, the errors, and a function that writes a tree of categories by calling itself.
Return value
Returns void. Nothing. An empty element is written as <a/>. An element with something inside is closed with </a>.
Errors
LogicErrorNo element is open. The document is not open.
Examples
Empty and filled elements
<?nvs// Shows the two ways `endElement` closes an element: `<a/>` when it is empty, and `</a>` when it is not.
Core\Xml\Writer $writer = Core\Xml::writer({ indent: ' ' });$writer->startDocument();$writer->startElement('cart');
// Nothing is written inside `empty`, so it is written as `<empty/>`.$writer->startElement('empty');$writer->endElement();
// `item` has an attribute, but nothing inside it. It is written as `<item sku="A-1"/>`.$writer->startElement('item');$writer->writeAttribute('sku', 'A-1');$writer->endElement();
// `note` has text inside, so it is closed with `</note>`.$writer->startElement('note');$writer->writeText('Please ring twice.');$writer->endElement();
$writer->endElement();echo $writer->endDocument(), "\n";<?xml version="1.0" encoding="UTF-8"?><cart> <empty/> <item sku="A-1"/> <note>Please ring twice.</note></cart>Closing too often or not at all
<?nvs// Shows the error when you close more elements than are open, and when you forget to close one.
Core\Xml\Writer $writer = Core\Xml::writer();$writer->startDocument();$writer->startElement('list');$writer->startElement('entry');
// `entry` is still open, so the document is not finished. This prints the error message.try { echo $writer->endDocument(), "\n";} catch (LogicError $error) { echo $error->message, "\n";}
$writer->endElement();$writer->endElement();
// Both elements are closed. A third `endElement` prints the error message.try { $writer->endElement();} catch (LogicError $error) { echo $error->message, "\n";}
echo $writer->endDocument(), "\n";Core\Xml\Writer::endDocument(): `<entry>` is still open, so the document is not complete.Core\Xml\Writer::endElement(): no element is open, so there is nothing to close.<?xml version="1.0" encoding="UTF-8"?><list><entry/></list>A category tree
<?nvs// Shows how a method that calls itself writes a tree of shop categories of any depth.
class Category{ public function constructor( public string $name, public array<Category> $children, ) {}
// Writes this category and all categories below it. Each call closes the element it opened. public function write(Core\Xml\Writer $writer): void { $writer->startElement('category'); $writer->writeAttribute('name', $this->name); foreach ($this->children as Category $child) { $child->write($writer); } $writer->endElement(); }}
Category $shop = new Category('Shop', [ new Category('Tea', [new Category('Green tea', []), new Category('Black tea', [])]), new Category('Coffee', []),]);
Core\Xml\Writer $writer = Core\Xml::writer({ indent: ' ' });$writer->startDocument();$shop->write($writer);echo $writer->endDocument(), "\n";<?xml version="1.0" encoding="UTF-8"?><category name="Shop"> <category name="Tea"> <category name="Green tea"/> <category name="Black tea"/> </category> <category name="Coffee"/></category>