2013-02-09 109 views
2

我有这样的代码:删除DOM警告PHP

$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文件有错误),所以有是删除此警告的一种方法? (没有出现)。

在此先感谢您的帮助。

回答

10

DOMDocument非常擅长处理不完善的标记,但它在处理时会抛出警告。

这里没有很好的记录。解决方案是实施 一个单独的设备来处理这些错误。

在调用loadHTML之前设置libxml_use_internal_errors(true)。这 将防止错误冒泡到您的默认错误处理程序。 然后您可以使用其他libxml错误 函数获得它们(如果您愿意)。

您可以在这里找到 http://www.php.net/manual/en/ref.libxml.php

更多信息处理DOM文档错误的正确方法是这样的:

<?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(); 
} 

?>