dongruan6001 2017-02-26 10:07
浏览 35
已采纳

在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;
$dom->LoadHTML($content);

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

1条回答 默认 最新

  • 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>
        <html>
        <body>
        Content of the document......
        </body>
        </html>
    ";
    
    $dom = new DOMDocument();
    $dom->loadHTML($content);
    
    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器