如何使用PHP加速XML DTD验证?

时间:2020-12-04 02:44:08

I am walidating my XML with a DTD file I have locally.

我用我在本地的DTD文件来验证我的XML。

For that, I am doing:

为此,我正在做:

$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);

And this takes about 30sec for an XML with 1600 lines.

对于1600行的XML,这需要大约30秒。

Is this a usual time? Should be much faster in my opinion?

这是平时吗?我认为应该快得多吗?

As you can see, the DTD I am using is locally on the server.

如您所见,我使用的DTD是本地服务器上的。

Any idea? Thank you.

任何想法?谢谢。

EDIT: 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.

编辑:通过调试和检查执行时间,我注意到如果我的xml有1600行或150行,它需要相同的时间,所以问题不是xml大小。

1 个解决方案

#1


2  

And this takes about 30sec for an XML with 1600 lines.

对于1600行的XML,这需要大约30秒。

That's an unusually long time, and it's likely due to misconfiguration.

这是一个异常漫长的时间,而且可能是由于配置错误造成的。

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.

通过调试和检查执行时间,我注意到如果我的xml有1600行或150行,则需要相同的时间,因此问题不在于xml大小。

For a tool that may provide more diagnostics here, try xmllint --valid. It will show, for example, errors for any DTDs that could not be retrieved.

对于可在此处提供更多诊断的工具,请尝试xmllint --valid。例如,它将显示无法检索的任何DTD的错误。

It's very likely that the extra time is due to fetching resources, such as the DTD, needed to perform validation.

额外的时间很可能是由于获取执行验证所需的资源(例如DTD)。

For one of your files, confirm that the URL of the DTD can be retrieved quickly by testing with a tool like curl from the same server. Is it a complex DTD? Does it bring in other files? Especially, make sure that it never refers to resources that would have to be fetched from the web, or with hostnames where DNS resolves slowly.

对于您的某个文件,请确认可以通过使用来自同一服务器的curl等工具进行快速检索,从而快速检索DTD的URL。它是一个复杂的DTD吗?它带来了其他文件吗?特别是,确保它永远不会引用必须从Web获取的资源,或者使用DNS缓慢解析的主机名。

#1


2  

And this takes about 30sec for an XML with 1600 lines.

对于1600行的XML,这需要大约30秒。

That's an unusually long time, and it's likely due to misconfiguration.

这是一个异常漫长的时间,而且可能是由于配置错误造成的。

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.

通过调试和检查执行时间,我注意到如果我的xml有1600行或150行,则需要相同的时间,因此问题不在于xml大小。

For a tool that may provide more diagnostics here, try xmllint --valid. It will show, for example, errors for any DTDs that could not be retrieved.

对于可在此处提供更多诊断的工具,请尝试xmllint --valid。例如,它将显示无法检索的任何DTD的错误。

It's very likely that the extra time is due to fetching resources, such as the DTD, needed to perform validation.

额外的时间很可能是由于获取执行验证所需的资源(例如DTD)。

For one of your files, confirm that the URL of the DTD can be retrieved quickly by testing with a tool like curl from the same server. Is it a complex DTD? Does it bring in other files? Especially, make sure that it never refers to resources that would have to be fetched from the web, or with hostnames where DNS resolves slowly.

对于您的某个文件,请确认可以通过使用来自同一服务器的curl等工具进行快速检索,从而快速检索DTD的URL。它是一个复杂的DTD吗?它带来了其他文件吗?特别是,确保它永远不会引用必须从Web获取的资源,或者使用DNS缓慢解析的主机名。