Search code examples
phpxmlsimplexml

PHP simplexml_load_file - catch file errors


Is it possible to catch simplexml file errors? I'm connecting to a webservice that sometimes fails, and I need to make the system skip a file if it returns some http error or something similar.


Solution

  • Using @ is just plain dirty.

    If you look at the manual, there is an options parameter:

    SimpleXMLElement simplexml_load_file ( string $filename [, string $class_name = "SimpleXMLElement" [, int $options = 0 [, string $ns = "" [, bool $is_prefix = false ]]]] )
    

    All option list is available here: http://www.php.net/manual/en/libxml.constants.php

    This is the correct way to suppress warnings:

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