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;
/*
Outputs :
<?xml version="1.0"?>
<a><b><c foo="
*/
?>
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;
/*
Outputs :
<?xml version="1.0"?>
<a><b><c foo=""/></b></a>
*/
?>