[英]How to handle 'simplexml_load_string' error?
I am looking to handle a situation whereby the XML file that I at trying to retrieve is not ready or is not available. 我正在设法解决一种情况,即我试图检索的XML文件尚未准备就绪或不可用。
I am using: 我在用:
$object = simplexml_load_string($file);
Sometimes, I get the following error: 有时,我得到以下错误:
'Start tag expected, '<' not found in etc...'
I have tried the following, and it doesn't work. 我已经尝试了以下方法,但是它不起作用。
if($object === false){
// code here
}
simplexml_load_string can return false if there is an error. 如果存在错误,则simplexml_load_string可以返回false。 It can also emit a warning.
它还可以发出警告。
Returns an object of class SimpleXMLElement with properties containing the data held within the xml document, or FALSE on failure.
返回具有包含包含在xml文档中的数据的属性的SimpleXMLElement类的对象,如果失败,则返回FALSE。
Produces an E_WARNING error message for each error found in the XML data.
为XML数据中发现的每个错误生成一条E_WARNING错误消息。
Use libxml_use_internal_errors() to suppress all XML errors, and libxml_get_errors() to iterate over them afterwards.
使用libxml_use_internal_errors()抑制所有XML错误,然后使用libxml_get_errors()遍历它们。
// suppress warnings so we can handle them ourselves
libxml_use_internal_errors(true);
$object = simplexml_load_string($file);
if ($object === false) {
// oh no
$errors = libxml_get_errors();
// do something with them
print_r($errors);
// really you'll want to loop over them and handle them as necessary for your needs
}
More detail on libxml_get_errors
here . 有关
libxml_get_errors
更多详细信息,请libxml_get_errors
此处 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.