如果 XML 无效,SimpleXML 将抛出异常
$xml = '<root><element>bad</element></root';
$sxml = new SimpleXMLElement($xml);
输出
<br />
<b>Warning</b>: SimpleXMLElement::__construct(): Entity: line 1: parser error : expected '>' in <b>[...][...]</b> on line <b>2</b><br />
<br />
<b>Warning</b>: SimpleXMLElement::__construct(): <root><element>bad</element></root in <b>[...][...]</b> on line <b>2</b><br />
<br />
<b>Warning</b>: SimpleXMLElement::__construct(): ^ in <b>[...][...]</b> on line <b>2</b><br />
<br />
<b>Fatal error</b>: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in [...][...]:2
Stack trace:
#0 [...][...](2): SimpleXMLElement->__construct('<root><element>...')
#1 {main}
thrown in <b>[...][...]</b> on line <b>2</b><br />