I'm fetching xml files from a server and sometimes I'm getting a non-valid xml files, because of this I'm getting a warning:
我从服务器获取xml文件,有时我得到一个无效的xml文件,因此我收到一个警告:
Warning: DOMDocument::load() [domdocument.load]: Start tag expected, '<' not found in
How can I catch this warning and delete the file?
如何捕获此警告并删除文件?
3 个解决方案
#1
36
You have two choices. Either use the @
error control operator in your load()
call, e.g. @$dom->load()
, which is somewhat slow because it globally changes the value of display_errors to off, executes the function and sets it back to on.
你有两个选择。在load()调用中使用@ error control运算符,例如@ $ dom-> load(),这有点慢,因为它全局地将display_errors的值更改为off,执行该函数并将其重新设置为on。
The other option, which I personally prefer (I hate the @
operator, I can't stand to see it in my code) is to save the old value of libxml_use_internal_errors, enable it using libxml_use_internal_errors(TRUE)
, call the function, clear the errors buffer and restore the old value. Here's a snippet from my code that does that:
另一个选项,我个人更喜欢(我讨厌@运算符,我无法忍受在我的代码中看到它)是保存libxml_use_internal_errors的旧值,使用libxml_use_internal_errors(TRUE)启用它,调用函数,清除错误缓冲并恢复旧值。这是我的代码中的代码段:
<?php
$previous_value = libxml_use_internal_errors(TRUE);
$doc->loadHTML((string)$e->response->getBody());
libxml_clear_errors();
libxml_use_internal_errors($previous_value);
I can't comment on answers yet, so I'll write it here:
我还不能评论答案,所以我会在这里写一下:
- Michael solution makes it less strict, but it'll still issue warnings for some of the errors:
- 迈克尔解决方案使其不那么严格,但它仍然会发出一些错误的警告:
nadav@shesek:~$ php -r '$dom=new DOMDocument; $dom->strictErrorChecking = FALSE ; $dom->loadHTML("<xy></zx>");' PHP Warning: DOMDocument::loadHTML(): Tag xy invalid in Entity, line: 1 in Command line code on line 1
- DON'T do what Fran Verona suggested - globally disabling error reporting is something you should never do. In production, set your own error handler and display a prettier message to the user, and make sure the error is logged somewhere - but never disable it completely. Setting error_reporting to 0 will cause PHP to disable error logging too.
- 不要做弗兰维罗纳建议的 - 全局禁用错误报告是你永远不应该做的事情。在生产中,设置自己的错误处理程序并向用户显示更漂亮的消息,并确保在某处记录错误 - 但永远不要完全禁用它。将error_reporting设置为0将导致PHP也禁用错误日志记录。
- Xeon06 solution is problematic because you're effecting the entire script error handler for a specific piece of code. Using your own error handler that simply ignores the error causes the same issues as Fran's solution.
- Xeon06解决方案存在问题,因为您正在影响特定代码段的整个脚本错误处理程序。使用您自己的错误处理程序只是忽略错误导致与Fran的解决方案相同的问题。
#3
2
Turn off strict error checking:
关闭严格的错误检查:
$dom = new DOMDocument();
$dom->strictErrorChecking = FALSE ;
$dom->load('/path/to/file.xml');
if (!$dom->validate()) {
// Invalid XML!
}
#1
36
You have two choices. Either use the @
error control operator in your load()
call, e.g. @$dom->load()
, which is somewhat slow because it globally changes the value of display_errors to off, executes the function and sets it back to on.
你有两个选择。在load()调用中使用@ error control运算符,例如@ $ dom-> load(),这有点慢,因为它全局地将display_errors的值更改为off,执行该函数并将其重新设置为on。
The other option, which I personally prefer (I hate the @
operator, I can't stand to see it in my code) is to save the old value of libxml_use_internal_errors, enable it using libxml_use_internal_errors(TRUE)
, call the function, clear the errors buffer and restore the old value. Here's a snippet from my code that does that:
另一个选项,我个人更喜欢(我讨厌@运算符,我无法忍受在我的代码中看到它)是保存libxml_use_internal_errors的旧值,使用libxml_use_internal_errors(TRUE)启用它,调用函数,清除错误缓冲并恢复旧值。这是我的代码中的代码段:
<?php
$previous_value = libxml_use_internal_errors(TRUE);
$doc->loadHTML((string)$e->response->getBody());
libxml_clear_errors();
libxml_use_internal_errors($previous_value);
I can't comment on answers yet, so I'll write it here:
我还不能评论答案,所以我会在这里写一下:
- Michael solution makes it less strict, but it'll still issue warnings for some of the errors:
- 迈克尔解决方案使其不那么严格,但它仍然会发出一些错误的警告:
nadav@shesek:~$ php -r '$dom=new DOMDocument; $dom->strictErrorChecking = FALSE ; $dom->loadHTML("<xy></zx>");' PHP Warning: DOMDocument::loadHTML(): Tag xy invalid in Entity, line: 1 in Command line code on line 1
- DON'T do what Fran Verona suggested - globally disabling error reporting is something you should never do. In production, set your own error handler and display a prettier message to the user, and make sure the error is logged somewhere - but never disable it completely. Setting error_reporting to 0 will cause PHP to disable error logging too.
- 不要做弗兰维罗纳建议的 - 全局禁用错误报告是你永远不应该做的事情。在生产中,设置自己的错误处理程序并向用户显示更漂亮的消息,并确保在某处记录错误 - 但永远不要完全禁用它。将error_reporting设置为0将导致PHP也禁用错误日志记录。
- Xeon06 solution is problematic because you're effecting the entire script error handler for a specific piece of code. Using your own error handler that simply ignores the error causes the same issues as Fran's solution.
- Xeon06解决方案存在问题,因为您正在影响特定代码段的整个脚本错误处理程序。使用您自己的错误处理程序只是忽略错误导致与Fran的解决方案相同的问题。
#2
#3
2
Turn off strict error checking:
关闭严格的错误检查:
$dom = new DOMDocument();
$dom->strictErrorChecking = FALSE ;
$dom->load('/path/to/file.xml');
if (!$dom->validate()) {
// Invalid XML!
}