我正在使用本地的 DTD 文件验证我的 XML。
为此,我正在做:
$xml = $dmsMerrin.'/xml/'.$id.'/conversion.xml';
$dtd = $dmsMerrin.'/style_files/journalpublishing.dtd';
$dom = new DOMDocument();
@$dom->load($xml);
libxml_use_internal_errors(true);
if (@$dom->validate()) {
$htmlDTDError .= "<h2>No Errors Found - The tested file is Valid !</h2>";
}
else {
$errors = libxml_get_errors();
$htmlDTDError .= '<h2>Errors Found ('.count($errors).')</h2><ol>';
foreach ($errors as $error) {
$htmlDTDError .= '<li>'.$error->message.' on line '.$error->line. '</li>';
}
$htmlDTDError .= '</ol>';
libxml_clear_errors();
}
libxml_use_internal_errors(false);
对于 1600 行的 XML,这大约需要 30 秒。
这是平时吗?我认为应该快得多?
如您所见,我使用的 DTD 在服务器本地。
有什么想法吗?谢谢。
编辑:通过调试和检查执行时间,我注意到如果我的 xml 有 1600 行或 150 行,它需要相同的时间,所以问题不是 xml 大小。
最佳答案
And this takes about 30sec for an XML with 1600 lines.
这段时间异常长,可能是配置错误造成的。
By debuging and checking the execution time, I noticed that it takes the same time if my xml has 1600 lines or 150 lines, so the problem is not the xml size.
有关可在此处提供更多诊断的工具,请尝试 xmllint --valid
。例如,它将显示无法检索的任何 DTD 的错误。
额外的时间很可能是由于获取执行验证所需的资源,例如 DTD。
对于您的其中一个文件,通过从同一台服务器使用 curl
等工具进行测试,确认可以快速检索 DTD 的 URL。它是一个复杂的 DTD 吗?它会引入其他文件吗?尤其要确保它永远不会引用必须从 Web 获取的资源,或者引用 DNS 解析缓慢的主机名。
关于php - 如何使用 PHP 加速 XML DTD 验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21859720/