php - 删除 DOM 警告 PHP

标签 php dom

我有这个代码:

$strhtml = file_get_contents('05001400300320100033100.html');
$dochtml = new DOMDocument();
 $dochtml->loadHTML($strhtml);
 $elm = $dochtml->getElementById('upPanelActuciones');
 print $dochtml->saveXml($elm);

我收到此警告:

      Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: error parsing attribute  name in Entity, line: 674 in C:\AppServ\www\video01\sector2\dom3.php on line 10

      Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: Opening and ending tag mismatch: div and td in Entity, line: 1019 in C:\AppServ\www\video01\sector2\dom3.php on line 10

      Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: Opening and ending tag mismatch: div and td in Entity, line: 1020 in C:\AppServ\www\video01\sector2\dom3.php on line 10

      Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: Opening and ending tag mismatch: div and td in Entity, line: 1022 in C:\AppServ\www\video01\sector2\dom3.php on line 10

我不能操作 html(我知道 html 文件有错误)所以有办法删除这个警告吗? (没有出现)。

预先感谢您的帮助。

最佳答案

DOMDocument is very good at dealing with imperfect markup, but it throws warnings all over the place when it does.

This isn't well documented here. The solution to this is to implement a separate aparatus for dealing with just these errors.

Set libxml_use_internal_errors(true) before calling loadHTML. This will prevent errors from bubbling up to your default error handler. And you can then get at them (if you desire) using other libxml error functions.

You can find more info here http://www.php.net/manual/en/ref.libxml.php

处理 DOMDocument 错误的正确方法是这样的:

<?php

// enable user error handling
var_dump(libxml_use_internal_errors(true));

// load the document
$doc = new DOMDocument;

if (!$doc->load('file.xml')) {
    foreach (libxml_get_errors() as $error) {
        // handle errors here
    }

    libxml_clear_errors();
}

?>

关于php - 删除 DOM 警告 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14783760/

相关文章:

javascript - 页面加载前的 Jquery 和 Ajax 脚本以防止闪烁

javascript - 为 Friconix 强制更新 JavaScript

javascript - 表 tr 背景颜色没有改变

javascript - JS-检查长度函数

javascript - Angular2 searchTerm 突出显示

javascript - HTML1506 : Unexpected token &lt;script&gt;

php - 多维数组格式化

php - 如何从 MySQL 反序列化值并使用 PHP/CodeIgniter 将值分配给变量?

php - 显示 2 位小数而不四舍五入

java - java和PHP之间使用static的区别?