5

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.

7 Answers 7

11

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the update, I believe some of the options listed weren't active in 2009, not sure though. I'm marking this as the correct answer anyway since it's the current best answer.
Tried. Does not catch or supress all warnings... Agree @ is plain dirty as well
6

You're talking about two different things. HTTP errors will have nothing to do with whether an XML file is valid, so you're looking at two separate areas of error handling.

You can take advantage of libxml_use_internal_errors() to suppress any XML parsing errors, and then check for them manually (using libxml_get_errors()) after each parse operation. I'd suggest doing it this way, as your scripts won't produce a ton of E_WARNING messages, but you'll still find the invalid XML files.

As for HTTP errors, handling those will depend on how you're connecting to the webservice and retrieving the data.

Comments

6

If you're not interested in error reporting or logging when the webservice fails you can use the error supression operator:

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

But this is a simple hack. A more robust solution would be to load the XML file with cURL, log any failed requests, parse any XML document returned with simplexml_load_string, log any XML parse errors and then do some stuff with the valid XML.

Comments

6

On error, your simplexml_load_file should return false.

So doing somethign as simple as this:

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

Is one way to detect errors.

1 Comment

almost, but you forgot to suppress the error on simplexml, like @pygorex1's example. Still, +1
2

You can set up an error handler within PHP to throw an Exception upon any PHP Errors: (Example and further documentation found here: PHP.net)

function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler("exception_error_handler");

2 Comments

I don't want to abort the system in case of error, just skip the function and continue the normal proccess
Well, by definition Exceptions do not abort execution. If you wrap your simplexml_load_file() within a try-catch block, you can intercept any error.
2

Another option is to use the libxml_use_internal_errors() function to capture the errors. The errors can then be retrieved using the libxml_get_errors() function. This will allow you to loop through them if you want to check what the specific errors are. If you do use this method, you will want to make sure that you clear the errors from memory when you are done with them so they are not wasting your memory space.

Here is an example:

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

Another example actually making use of the errors we captured:

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

Comments

0
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);
}

2 Comments

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);
XML file:samplexml.xml<?xml version="1.0"?> <xml> <draw> <candelete>yes</candelete> <forpayroll>no</forpayroll> <name>hello</name> </draw> <draw> <candelete>yes2</candelete> <forpayroll>no3</forpayroll> <name>hello3</name> </draw> </xml>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.