PHP捕获SimpleXMLElement解析错误 [英] PHP Catching a SimpleXMLElement parse error
问题描述
我有一个解析一些XML(adf)内容的脚本.有时我们会收到损坏的XML数据(即语法,无结束标记等).
I have a script that parses some XML (adf) stuff. Sometimes we receive broken XML data (ie- syntax, no ending tag, etc.).
SimpleXMLElement
抛出错误并杀死了我的脚本,如何分配类似$xml_body = new SimpleXMLElement ($adf_xml);
的内容并捕获解析异常?
SimpleXMLElement
throws an error and kills my script, how could assign something like $xml_body = new SimpleXMLElement ($adf_xml);
and catch the parse exception?
/home//Work//script/email_leads.php:46中未捕获的消息字符串无法解析为XML"的异常"Exception"
堆栈跟踪:
0/home//Work//script/email_leads.php(46):SimpleXMLElement-> __ construct('<?xml version ="...')
1/home//Work//script/email_leads.php(97):generateFeed()
2 {main}
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}
推荐答案
好吧,因此显然捕获XML Parse错误似乎是一个圣杯……我最终还是
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; }
这篇关于PHP捕获SimpleXMLElement解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!