PHP 捕获 SimpleXMLElement 解析错误
我有一个脚本可以解析一些 XML (adf) 内容。有时我们会收到损坏的 XML 数据(即语法、没有结束标记等)。
SimpleXMLElement
抛出错误并终止我的脚本,如何分配类似 $xml_body = new SimpleXMLElement ($adf_xml);
的内容并捕获解析异常?
/home//Work//script/email_leads.php:46 中未捕获异常“异常”,消息为“字符串无法解析为 XML”
堆栈跟踪:
0 /home//Work//script/email_leads.php(46): SimpleXMLElement->__construct(' 1 /home//Work//script/email_leads.php(97):generateFeed()
2 {主要}
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}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,显然捕获 XML 解析错误在某种程度上是一个圣杯......我最终只是
编辑:感谢 @PanPipes
Ok, so apparently catching XML Parse errors is somewhat of a Holy Grail... I ended up just
EDIT: thanks to @PanPipes
xml_parse
返回一个布尔值,指示 XML 是否已成功解析。因此,这应该有效:xml_parse
returns a boolean value indicating whether the XML has been parsed successfully. Therefore, this should work: