PHP simplexml_加载_文件-捕获文件错误

PHP simplexml_加载_文件-捕获文件错误,php,xml,simplexml,Php,Xml,Simplexml,是否可以捕获simplexml文件错误?我正在连接一个有时会失败的Web服务,如果某个文件返回http错误或类似错误,我需要让系统跳过该文件。你说的是两件不同的事情。HTTP错误与XML文件是否有效无关,因此您将看到错误处理的两个不同方面 您可以利用来抑制任何XML解析错误,然后在每次解析操作后手动(使用)检查这些错误。我建议这样做,因为您的脚本不会产生大量的E_警告消息,但仍然会找到无效的XML文件 至于HTTP错误,处理这些错误将取决于您连接到Web服务和检索数据的方式。您可以在PHP中设置

是否可以捕获simplexml文件错误?我正在连接一个有时会失败的Web服务,如果某个文件返回http错误或类似错误,我需要让系统跳过该文件。

你说的是两件不同的事情。HTTP错误与XML文件是否有效无关,因此您将看到错误处理的两个不同方面

您可以利用来抑制任何XML解析错误,然后在每次解析操作后手动(使用)检查这些错误。我建议这样做,因为您的脚本不会产生大量的
E_警告消息,但仍然会找到无效的XML文件


至于HTTP错误,处理这些错误将取决于您连接到Web服务和检索数据的方式。

您可以在PHP中设置一个错误处理程序,以便在任何PHP错误时引发异常:(示例和进一步的文档见此:)


出现错误时,simplexml\u加载\u文件应返回false

所以做一些简单的事情如下:

   $xml = @simplexml_load_file('myfile');
   if (!$xml) {
      echo "Uh oh's, we have an error!";
   }

是检测错误的一种方法。

如果您对Web服务失败时的错误报告或日志记录不感兴趣,可以使用错误抑制操作符:

$xml= @simplexml_load_file('http://tri.ad/test.xml');
if ($xml) {
 // Do some stuff . . .
}

但这是一个简单的黑客。一个更健壮的解决方案是加载XML文件,记录任何失败的请求,解析任何使用
simplexml\u load\u string
返回的XML文档,记录所有XML解析错误,然后使用有效的XML执行一些操作。

使用
@
完全是肮脏的

如果您查看手册,其中有一个选项参数:

SimpleXMLElement simplexml_load_file ( string $filename [, string $class_name = "SimpleXMLElement" [, int $options = 0 [, string $ns = "" [, bool $is_prefix = false ]]]] )
所有选项列表在此处可用:

这是抑制警告的正确方法:

$xml = simplexml_load_file('file.xml', 'SimpleXMLElement', LIBXML_NOWARNING);

另一个选项是使用
libxml\u use\u internal\u errors()
函数捕获错误。然后可以使用
libxml\u get\u errors()
函数检索错误。如果您想检查具体的错误是什么,这将允许您循环检查它们。如果您确实使用此方法,您将希望确保在处理完错误后从内存中清除这些错误,以便它们不会浪费内存空间

以下是一个例子:

<?php
    //Store errors in memory rather than outputting them
    libxml_use_internal_errors(true);

    $xml = simplexml_load_file('myfile.xml');

    if (!$xml){
        //Exit because we can't process a broken file
        exit;
    }

    //Important to clear the error buffer
    libxml_clear_errors();

    //Display your xml code
    print_r($xml);

我不想在出现错误时中止系统,只需跳过该函数并继续正常过程即可。根据定义,异常不会中止执行。如果将simplexml_load_file()包装在一个try-catch块中,则可以截获任何错误。几乎,但您忘记了抑制simplexml上的错误,如@pygorex1的示例。尽管如此,+1感谢更新,我相信列出的一些选项在2009年并不活跃,但不确定。不管怎样,我都会将此标记为正确答案,因为这是当前最好的答案。已尝试过。不会捕获或抑制所有警告。。。Agree@也非常脏,每个($xml as$syn){$candelete=$syn->candelete;$forpayroll=$syn->forpayroll;$name=$syn->name;$sql=“插入vtiger(candelete,forpayroll,name)值('$candelete','$forpayroll','$name')”;$query=mysql\u查询($sql);xml文件:samplexml.xml是否hello yes2 no3 hello3
if (!$xml=simplexml_load_file('./samplexml.xml')) {  
    trigger_error('Error reading XML file',E_USER_ERROR);
}

foreach ($xml as $syn) {
    $candelete = $syn->candelete;
    $forpayroll = $syn->forpayroll;
    $name = $syn->name;
    $sql = "INSERT INTO vtiger (candelete, forpayroll, name) VALUES('$candelete','$forpayroll','$name')";
    $query = mysql_query($sql);
}
<?php
    //Store errors in memory rather than outputting them
    libxml_use_internal_errors(true);

    $xml = simplexml_load_file('myfile.xml');

    if (!$xml){
        //Exit because we can't process a broken file
        exit;
    }

    //Important to clear the error buffer
    libxml_clear_errors();

    //Display your xml code
    print_r($xml);
<?php
    //Store errors in memory rather than outputting them
    libxml_use_internal_errors(true);

    $xml = simplexml_load_file('myfile.xml');

    if (!$xml){

        echo "Your script is not valid due to the following errors:\n";

        //Process error messages
        foreach(libxml_get_errors() as $error){
           echo "$error";
        }

        //Exit because we can't process a broken file
        exit;
    }

    //Important to clear the error buffer
    libxml_clear_errors();

    //Display your xml code
    print_r($xml);