How to check if an XML file is valid with PHP?

Asked

Viewed 785 times

10

As I understand the answers of this question from the OS, the XMLReader::isValid() checks "only the current node, not the whole document". What does that mean?

In case I want to validate if xml is valid before doing:

$meuXml = simplexml_load_string($meuXml);

Because sometimes I think the xml comes poorly formed, so I get a type error:

stderr: PHP message: PHP Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<

So basically the question is : What is the best way to verify a document .xml is valid using PHP? But I would also like to know what "checking only the current node, not the entire document".

2 answers

6


Just do the error handling with this same PHP XML API.

The function simplexml_load_string returns false when you cannot correctly process the XML, then just analyze the function return.

$meuXml = simplexml_load_string($meuXml);

if (!$meuXml) {
    die("Erro ao processar o XML");
}

But depending on your server settings, the Warning will be displayed in the browser. In order for it not to be displayed, you can inform that you will handle it internally with the function libxml_use_internal_errors and libxml_get_errors:

libxml_use_internal_errors(true);

$meuXml = simplexml_load_string($meuXml);

if (!$meuXml) {
    $erros = libxml_get_errors();

    foreach ($erros as $erro) {
        echo $erro->message, PHP_EOL;
    }

    exit;
}

Thus, if XML is not valid, it would produce an output:

Erro: Start tag expected, '<' not found

And you can treat mistakes as you wish.

  • Anderson, in this case already evaluates the whole XML or just one of the nodes... btw I haven’t quite understood the question of the nodes, if you can give a brief explanation...

  • 1

    @gustavox XML in $meuXml whole. The return of libxml_get_errors will be a array with all the mistakes.

2

XML is made of multiple nodes when you use the function XMLReader::isValid(), it validates only the node passed by parameter and not the "sub-nodes" that this node contains.

What you can do is validate XML along with your XSD using Domdocument::schemaValidate.

If you don’t have xsd, you can make an exception treatment on simplexml_load_string and show a custom message.

What you can do too, is check the validity of the document in the w3schools to find out if the problem really is xml.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.