The following code creates an XML file, but the last line is blank which causes problems when validated.
How can I change the following code so that the outputted file does not have a blank line at the end of it?
<?php
$xmlFileName = 'testoutput.xml';
$xml = new XMLWriter;
$xml->openURI($xmlFileName);
$xml->startDocument('1.0', 'UTF-8');
$xml->setIndent(1);
$xml->startElement('name');
$xml->text('jim');
$xml->endElement();
$xml->endDocument();
$xml->flush();
?>
@DavidRR, the validation problem comes when I validate the XML file with the following code, it tells me that there is "extra content at the end of the document":
$schema = 'test.xsd';
$files[] = 'test1.xml';
$files[] = 'test2.xml';
foreach ($files as $file) {
validateXml($file, $schema);
}
function validateXml($xmlFile, $xsdFile) {
$dom = new DOMDocument;
$dom->load($xmlFile);
libxml_use_internal_errors(true); // enable user error handling
echo "Validating <b>$xmlFile</b> with <b>$xsdFile</b>:";
if ($dom->schemaValidate($xsdFile)) {
echo '<div style="margin-left:20px">ok</div>';
} else {
$errors = libxml_get_errors();
if (count($errors) > 0) {
echo '<ul style="color:red">';
foreach ($errors as $error) {
//var_dump($error);
echo '<li>' . $error->message . '</li>';
}
echo '</ul>';
}
libxml_clear_errors();
echo '</span>';
libxml_use_internal_errors(false); // enable user error handling
}
}