SimpleXML with all of it’s faults is still a great way to interact with XML. Most of it’s shortcomings are related to debugging and how it handles invalid XML is no exception. Let’s take a look at what happens when we load an invalid XML string:
This will raise the exception because the declaration is malformed and we have an unclosed child node as well. The only indication that SimpleXML will give us is our object not being instantiated and being set to boolean false.
To coerce SimpleXML into playing nice and giving us some errors we can work with we have to set use internal flags to true. Once we do that we can interrogate another function and expose the error message(s).
This will raise an exception under the same circumstance, but now we have a verbose error message that we can use to troubleshoot the situation!