Debugging Invalid XML in PHP

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:

$xml = " <?xml version='1.0'><root><unclosed></root>"

$simple_xml = simplexml_load_string($xml

if ($simple_xml === false)
{
    throw new Exception('Invalid XML'
}

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).

$xml = " <?xml version='1.0'><root><unclosed></root>"

libxml_use_internal_errors(true
$simple_xml = simplexml_load_string($xml

if ($simple_xml === false)
{
    $message = 'Invalid XML: '

    foreach (libxml_get_errors() as $error)
    {
        $message .= "n" . $error->message
    }

    throw new Exception($message
}

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!

Josh Sherman - The Man, The Myth, The Avatar

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.


If you found this article helpful, please consider buying me a coffee.