以下代码创建了一个XML文件,但最后一行为空,这会在验证时导致问题。
如何更改以下代码,使输出的文件末尾没有空行?
<?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,当我用以下代码验证XML文件时,验证问题就来了,它告诉我"文档结尾有额外的内容":
$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
}
}
报告的问题:由于XML文件末尾有一个空行,对该文件的架构验证尝试导致错误:
"Extra content at the end of the document"
我无法在codepad、PHP版本5.4-dev或该网站上的任何早期版本中重现您所说的问题。我在这里也包括了你的代码的编辑版本。(我的版本包括创建正在检查的简单XSD和XML文件的功能。)
可能性:您的问题是否与您使用的PHP版本有关?
如果我没有用我对你的代码的改编来准确地测试你的场景,请进一步修改我的代码以解决问题。
<?php
$xsdFile = sys_get_temp_dir() . '/test1.xsd';
$xmlFile = sys_get_temp_dir() . '/test1.xml';
createXsdFile($xsdFile);
createXmlFile($xmlFile);
$files[] = $xmlFile;
foreach ($files as $file) {
validateXml($file, $xsdFile);
}
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
}
}
function createXsdFile($xsdFile) {
$file = fopen($xsdFile, 'w');
fwrite($file, "<?xml version='1.0' encoding='utf-8'?>'n");
fwrite($file, "<schema xmlns='http://www.w3.org/2001/XMLSchema'>'n");
fwrite($file, "<element name='name' type='string' />'n");
fwrite($file, "</schema>'n");
fclose($file);
}
//
// Appends a blank line at the end of the XML file.
// Does this cause a schema validation problem?
//
function createXmlFile($xmlFile) {
$xml = new XMLWriter;
$xml->openURI($xmlFile);
$xml->startDocument('1.0', 'UTF-8');
$xml->setIndent(1);
$xml->startElement('name');
$xml->text('jim');
$xml->endElement();
$xml->endDocument();
$xml->flush();
}
?>
我没有找到任何方法来改变XmlWriter
在这方面的行为。一个可能的解决方案是读取文件,trim
,然后将其写回文件,例如
file_put_contents($xmlFileName, trim(file_get_contents($xmlFileName)));
演示
另一种选择是ftruncate
文件
ftruncate(fopen($xmlFileName, 'r+'), filesize($xmlFileName) - strlen(PHP_EOL));
演示
后者假设文件中会有一个依赖于平台的换行符。如果没有,这很可能会破坏文件。trim
版本在这方面更可靠,因为如果没有换行符,它不会损坏文件,但它必须将整个文件读取到内存中才能修剪内容。
如果您在linux/unix系统上,您可以执行:
$test = `head -n -1 < $xmlFileName > $xmlFileName`;
看看这个。