Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Catching a SimpleXMLElement parse error [closed]

Tags:

php

xml

simplexml

I have a script that parses some XML (adf) stuff. Sometimes we receive broken XML data (ie- syntax, no ending tag, etc.).

SimpleXMLElement throws an error and kills my script, how could assign something like $xml_body = new SimpleXMLElement ($adf_xml); and catch the parse exception?


Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home//Work//script/email_leads.php:46
Stack trace:
0 /home//Work//script/email_leads.php(46): SimpleXMLElement->__construct('<?xml version="...')
1 /home//Work//script/email_leads.php(97): generateFeed()
2 {main}

like image 727
Weston Watson Avatar asked Nov 09 '10 19:11

Weston Watson


2 Answers

Ok, so apparently catching XML Parse errors is somewhat of a Holy Grail... I ended up just

try { $x = new SimpleXMLElement($y, LIBXML_NOERROR); } catch (Exception $e) { echo $e; }

EDIT: thanks to @PanPipes

like image 118
Weston Watson Avatar answered Oct 27 '22 02:10

Weston Watson


libxml_use_internal_errors(true);
like image 28
bcosca Avatar answered Oct 27 '22 02:10

bcosca