tags:

views:

154

answers:

3

I have a page that will basically be used to concatenate a bunch of xml files, it will act as glue that binds them together. There's a small chance the xml files that are being combined might not be well formed because the user will have access.

I'm trying to basically rewrite a live file if there are no warnings / errors thrown in a specific set of code.

So far I have:

try {
    $first = simplexml_load_file( 'file.xml' );
} catch ( Exception $e ) {
    $write = false;
}

if ( !$write ) { 
// write to live file.
}

This obviously catches error exceptions, but sometimes function invocations can return warnings and not errors per se, what can I use to catch errors, basically only write if no warnings and errors have been thrown in the try block?

Example of a warning being thrown:

Warning: simplexml_load_file() parser error : Start tag expected, '<'
+3  A: 

A few things come to mind for this situation. if you will always have warnings enabled (not a great idea for a production server) you can always use the output buffering functions to check if there was any text sent out

ob_start();
... //code
$str = ob_get_contents();
if (! empty($string))
{
  // a warning was thrown.
}

The better way is to define an error handler for warnings and do whatever you want with them:

set_error_handler("my_warning_handler", E_WARNING);

function my_warning_handler($errno, $errstr) {
   // do something
}
Byron Whitlock
+4  A: 

Just look for other "symptoms" of a failed read. From the simplexml_load_file manual entry:

Return Values

Returns an object of class SimpleXMLElement with properties containing the data held within the XML document. On errors, it will return FALSE.

So, use it like this:

$first = simplexml_load_file( 'file.xml' );

if ($first == false) {
    echo "File couldn't be loaded";
    exit(); // abort normal program execution, redirect or what have you
}

// continue as normal

Almost all functions return some sort of false in case of an error. You shouldn't use manual "error parsing" except for very special cases. This isn't one of them.

deceze
yeah, but this gets tedious if you have lots of instances, no? even if you combine all the checking into one expression. i'm not worried about performance issues either.
meder
+1 good answer.
Byron Whitlock
Do you mean `if ($var == false)` is more tedious than handling exceptions or output buffering and string parsing? This is by far the simplest method if your function helpfully returns a `false`. If you do this multiple times, gather all results in an array and check if there are any `false` entries in the array once at the end. Or do it in a loop.
deceze
It's just that it's a whole slew of expressions, not just assigning a simple identifier to something that coerces to true, I'm doing loops through it, and I'm doing a lot of operation which is why I don't want to have 10-15 or so checks.
meder
Please post more of your code and why you think it's so hard to integrate.
deceze
+1  A: 

Set an error handler, and raise exceptions from there. Then catch them as usual.

o_O Tync