2017-02-26 10:07
浏览 28

在XML上使用DOMDocument Validate()会导致无限负载

I have an extremely simple implementation that pulls in a test bit of XML and attempts to validate it using DOMDocument. In testing, it's able to get through the LoadHTML() call fine, but as soon as I try and run validate(), the browser hangs forever and doesn't load. Here's the code:

$content = '<?xml version="1.0" encoding="utf-8"?><mainElement></mainElement>';

$dom = new DOMDocument;

if (!$dom->validate()) {
    echo 'fail';
} else {
    echo 'success!';

图片转代码服务由CSDN问答提供 功能建议

我有一个非常简单的实现,它会引入XML的测试位并尝试使用DOMDocument对其进行验证。 在测试中,它能够很好地完成LoadHTML()调用,但是一旦我尝试运行validate(),浏览器就会永远挂起并且不会加载。 这是代码:

  $ content ='&lt;?xml version =“1.0”encoding =“utf-8”?&gt;&lt; mainElement&gt;&lt; / mainElement&gt;  '; 
 $ dom =新的DOMDocument; 
 $ dom-&gt; LoadHTML($ content); 
if(!$ dom-&gt; validate()){
  } else {
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • drv16821
    drv16821 2017-02-26 12:51

    It seems that if you want to validate content loaded with loadHTML, you need DOCTYPE declaration (without it, you get an infinitive loop). For example, following code works and prints fail

    $content = "
        <!DOCTYPE html>
        Content of the document......
    $dom = new DOMDocument();
    if (!$dom->validate()) {
        echo 'fail';
    } else {
        echo 'success!';

    For XML it's more tolerant (it works even you didn't declare dtd but it returns false). In your case, you might use loadXML method and your code will print fail.

    Tested with php 7.0.13.

    点赞 评论