Question: 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}
> 
> 
>

Answer: 
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
`xml_parse` returns a boolean value indicating whether the XML has been parsed successfully. Therefore, this should work:

```
$fp = fopen($xml_file, "r");
$xml_data = fread($fp, 80000);

if(!(xml_parse($xml_parser, $xml_data, feof($fp)))){
    # do something
} 

```