Core\Xml\Writer::startElement
Opens an element, such as <order>. Everything you write after this call goes inside the element, until you close it with endElement. You can open an element inside another one, and so build a document of any shape.
Right after startElement, you can add attributes to the element with writeAttribute. The name must be a valid XML name, such as order, _id or atom:link. The writer does not escape a name, so a name with a space or a < throws a LogicError.
A document has exactly one root element. When the root element is closed, a second startElement throws a LogicError. At most 1024 elements can be open inside each other. Opening one more also throws a LogicError.
The examples below build a nested document, show the errors, and write a sitemap for a website.
Parameters
$namestringThe element's name, such asorderoratom:link. It must be a valid XML name. The writer does not escape it.
Return value
Returns void. Nothing. The element is open, and the next calls write inside it.
Errors
LogicErrorThe name is not a valid XML name. The root element is already written and closed. 1024 elements are already open inside each other. The document is not open.
Examples
Elements inside elements
<?nvs// Shows how to open elements inside other elements to build a nested document.
Core\Xml\Writer $writer = Core\Xml::writer({ indent: ' ' });$writer->startDocument();
// `order` is the root element. Everything below goes inside it.$writer->startElement('order');$writer->writeAttribute('id', '1042');
// `customer` is inside `order`, and `name` is inside `customer`.$writer->startElement('customer');$writer->startElement('name');$writer->writeText('Anna Berger');$writer->endElement();$writer->endElement();
$writer->startElement('total');$writer->writeText('25.90');$writer->endElement();
$writer->endElement();echo $writer->endDocument(), "\n";<?xml version="1.0" encoding="UTF-8"?><order id="1042"> <customer> <name>Anna Berger</name> </customer> <total>25.90</total></order>Names and roots that are not allowed
<?nvs// Shows the errors `startElement` throws for a wrong name and for a second root element.
Core\Xml\Writer $writer = Core\Xml::writer();$writer->startDocument();
// A name cannot start with a digit or contain a space. Each call prints the error message.array<string> $names = ['2nd-line', 'first name'];foreach ($names as string $name) { try { $writer->startElement($name); } catch (LogicError $error) { echo $error->message, "\n"; }}
$writer->startElement('list');$writer->endElement();
// The root element `list` is closed. A second root element is not allowed.try { $writer->startElement('list');} catch (LogicError $error) { echo $error->message, "\n";}
echo $writer->endDocument(), "\n";Core\Xml\Writer::startElement(): `2nd-line` is not a name a document can write.Core\Xml\Writer::startElement(): `first name` is not a name a document can write.Core\Xml\Writer::startElement(): a document has one root element, and it is already written.<?xml version="1.0" encoding="UTF-8"?><list/>Write a sitemap
<?nvs// Shows how to write a sitemap, the file that tells a search engine which pages a website has.
array<string> $pages = [ 'https://example.com/' => '2026-09-01', 'https://example.com/shop' => '2026-09-20', 'https://example.com/contact' => '2026-03-15',];
Core\Xml\Writer $writer = Core\Xml::writer({ indent: ' ' });$writer->startDocument();$writer->startElement('urlset');$writer->writeAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
// Each page gets a `url` element with its address and the date it last changed.foreach ($pages as string $address => string $changed) { $writer->startElement('url'); $writer->startElement('loc'); $writer->writeText($address); $writer->endElement(); $writer->startElement('lastmod'); $writer->writeText($changed); $writer->endElement(); $writer->endElement();}
$writer->endElement();echo $writer->endDocument(), "\n";<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <loc>https://example.com/</loc> <lastmod>2026-09-01</lastmod> </url> <url> <loc>https://example.com/shop</loc> <lastmod>2026-09-20</lastmod> </url> <url> <loc>https://example.com/contact</loc> <lastmod>2026-03-15</lastmod> </url></urlset>