Skip to content

Core\Xml\Writer::endDocument

$writer->endDocument(): string

Finishes the document and returns it as one string. This is the only method of the writer that returns something. You can print the string, send it as a response, or save it to a file.

Before it returns, endDocument checks the document. If an element is still open, it throws a LogicError that names the element. It also throws a LogicError when no root element was written. So the string you get is always a complete document that an XML parser can read.

After endDocument, the writer is finished. Every other call on it throws a LogicError.

The examples below show the returned string, the errors, and a function that builds a sitemap and returns it.

Return value

Returns string. The whole document as a string. The string is not tainted, because the writer escaped every text and checked every name. After this call, every other method of the writer throws an error.

Errors

  • LogicErrorAn element is still open. No root element was written. The document was not started with startDocument. The document is already finished.

Examples

The returned string

<?nvs
// Shows that `endDocument` returns the whole document as one string.
Core\Xml\Writer $writer = Core\Xml::writer();
$writer->startDocument();
$writer->startElement('greeting');
$writer->writeText('Hello');
$writer->endElement();
// `endDocument` returns the document. Nothing is printed until you print the string.
string $document = $writer->endDocument();
// This prints the document, and then its length: 64.
echo $document, "\n";
echo Core\Str::length($document), "\n";
Output
<?xml version="1.0" encoding="UTF-8"?><greeting>Hello</greeting>
64

A document that is not complete

<?nvs
// Shows the errors when you finish a document without a root element, or with an element still open.
Core\Xml\Writer $writer = Core\Xml::writer();
$writer->startDocument();
// No element is written yet. This prints the error message.
try {
echo $writer->endDocument(), "\n";
} catch (LogicError $error) {
echo $error->message, "\n";
}
$writer->startElement('order');
$writer->startElement('item');
// `item` is still open. This prints the error message, which names `item`.
try {
echo $writer->endDocument(), "\n";
} catch (LogicError $error) {
echo $error->message, "\n";
}
// The errors did not change the writer. After both elements are closed, the document is complete.
$writer->endElement();
$writer->endElement();
echo $writer->endDocument(), "\n";
// The document is finished. A second `endDocument` prints the error message.
try {
echo $writer->endDocument(), "\n";
} catch (LogicError $error) {
echo $error->message, "\n";
}
Output
Core\Xml\Writer::endDocument(): a document has a root element, and none was written.
Core\Xml\Writer::endDocument(): `<item>` is still open, so the document is not complete.
<?xml version="1.0" encoding="UTF-8"?><order><item/></order>
Core\Xml\Writer::endDocument(): the document is finished, and `endDocument` was already called.

A sitemap

<?nvs
// Shows a function that builds a sitemap for search engines and returns it as a string.
class Sitemap
{
// Writes one `url` element for each page, and returns the finished document.
public static function of(string $site, array<string> $pages): string
{
Core\Xml\Writer $writer = Core\Xml::writer({ indent: ' ' });
$writer->startDocument();
$writer->startElement('urlset');
$writer->writeAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
foreach ($pages as string $page) {
$writer->startElement('url');
$writer->startElement('loc');
$writer->writeText($site . $page);
$writer->endElement();
$writer->endElement();
}
$writer->endElement();
return $writer->endDocument();
}
}
string $sitemap = Sitemap::of('https://example.com', ['/', '/shop', '/contact']);
echo $sitemap, "\n";
// The string is a complete document. `Core\Xml::parse` reads it without an error.
Core\Xml::parse($sitemap);
echo "The sitemap is complete.\n";
Output
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://example.com/</loc>
</url>
<url>
<loc>https://example.com/shop</loc>
</url>
<url>
<loc>https://example.com/contact</loc>
</url>
</urlset>
The sitemap is complete.