XMLWriter::endDocument() closes properly all remaining elements not yet closed.
Without XMLWriter::endDocument() :
<?php
$xml = new XMLWriter();
$xml->openUri('php://stdout');
$xml->startDocument();
$xml->startElement('a');
$xml->startElement('b');
$xml->startElement('c');
$xml->startAttribute('foo');
exit;
?>
With XMLWriter::endDocument() :
<?php
$xml = new XMLWriter();
$xml->openUri('php://stdout');
$xml->startDocument();
$xml->startElement('a');
$xml->startElement('b');
$xml->startElement('c');
$xml->startAttribute('foo');
$xml->endDocument();
exit;
?>