我有这个代码:
$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/