Skip to content

Core\Xml\Writer::startDocument

$writer->startDocument(): void

Starts a new XML document. It writes the XML declaration, the first line of every document: <?xml version="1.0" encoding="UTF-8"?>. You call it once, before any other method of the writer.

A writer writes exactly one document. If you call startDocument a second time, it throws a LogicError. This is also true after endDocument has finished the document. To write a second document, create a new writer with Core\Xml::writer().

Every other method of the writer needs a started document. If you call startElement before startDocument, it throws a LogicError.

The examples below show the declaration, the errors, and a loop that writes one document for each customer.

Return value

Returns void. Nothing. After this call, you can write the root element.

Errors

  • LogicErrorThe document is already started. The document is already finished with endDocument.

Examples

The declaration

<?nvs
// Shows the smallest document a writer can write: the declaration and one empty element.
Core\Xml\Writer $writer = Core\Xml::writer();
// `startDocument` writes the declaration `<?xml version="1.0" encoding="UTF-8"?>`.
$writer->startDocument();
$writer->startElement('ping');
$writer->endElement();
// This prints the declaration, followed by `<ping/>`.
echo $writer->endDocument(), "\n";
Output
<?xml version="1.0" encoding="UTF-8"?><ping/>

Too early and too often

<?nvs
// Shows the errors when you write before `startDocument`, and when you call it twice.
Core\Xml\Writer $writer = Core\Xml::writer();
// The document is not started yet. This prints the error message.
try {
$writer->startElement('note');
} catch (LogicError $error) {
echo $error->message, "\n";
}
$writer->startDocument();
// The document is already started. This prints the error message.
try {
$writer->startDocument();
} catch (LogicError $error) {
echo $error->message, "\n";
}
$writer->startElement('note');
$writer->endElement();
echo $writer->endDocument(), "\n";
// The document is finished. A writer writes only one document, so this prints the error message.
try {
$writer->startDocument();
} catch (LogicError $error) {
echo $error->message, "\n";
}
Output
Core\Xml\Writer::startElement(): the document is not open, and `startDocument` is what opens it.
Core\Xml\Writer::startDocument(): the document is already open.
<?xml version="1.0" encoding="UTF-8"?><note/>
Core\Xml\Writer::startDocument(): the document is finished, and `endDocument` was already called.

One document per customer

<?nvs
// Shows how a program writes one XML file for each customer, with a new writer for each file.
class Invoice
{
// Returns the invoice of one customer as a complete XML document.
public static function write(string $customer, int $total): string
{
Core\Xml\Writer $writer = Core\Xml::writer({ indent: ' ' });
$writer->startDocument();
$writer->startElement('invoice');
$writer->writeAttribute('customer', $customer);
$writer->startElement('total');
$writer->writeText($total as string);
$writer->endElement();
$writer->endElement();
return $writer->endDocument();
}
}
// Each call creates its own writer, so each invoice starts with its own declaration.
echo Invoice::write('Ada', 120), "\n\n";
echo Invoice::write('Linus', 75), "\n";
Output
<?xml version="1.0" encoding="UTF-8"?>
<invoice customer="Ada">
<total>120</total>
</invoice>
<?xml version="1.0" encoding="UTF-8"?>
<invoice customer="Linus">
<total>75</total>
</invoice>