PHPKonf: Istanbul PHP Conference 2017

Traitement des erreurs XML

Le traitement des erreurs XML lors du chargement d'un document est une tâche simple. En utilisant les fonctionnalités libxml, il est possible de supprimer toutes les erreurs XML lors du chargement d'un document, puis, de les parcourir.

L'objet libXMLError, retourné par la fonction libxml_get_errors(), contient plusieurs propriétés dont le message, la ligne et la colonne (position) de l'erreur.

Exemple #1 Chargement de chaînes XML cassées

$sxe simplexml_load_string("<?xml version='1.0'><broken><xml></broken>");
if (
$sxe === false) {
"Erreur lors du chargement du XML\n";
libxml_get_errors() as $error) {

L'exemple ci-dessus va afficher :

Erreur lors du chargement du XML
    Blank needed here
    parsing XML declaration: '?>' expected
    Opening and ending tag mismatch: xml line 1 and broken
    Premature end of data in tag broken line 1

add a note add a note

User Contributed Notes 4 notes

openbip at gmail dot com
6 years ago
Note that "if (! $sxe) {" may give you a false-negative if the XML document was empty (e.g. "<root />").  In that case, $sxe will be:

object(SimpleXMLElement)#1 (0) {

which will evaluate to false, even though nothing technically went wrong.

Consider instead: "if ($sxe === false) {"
2 years ago
Now that the /e modifier is considered deprecated in preg_replace, you can use a negative lookahead to replace unescaped ampersands with &amp; without throwing warnings:

$str = preg_replace('/&(?!;{6})/', '&amp;', $str);

You probably should have been doing this before /e was deprecated, actually.
Jacob Tabak
6 years ago
If you are trying to load an XML string with some escaped and some unescaped ampersands, you can pre-parse the string to ecsape the unescaped ampersands without modifying the already escaped ones:
= preg_replace('/&[^; ]{0,6}.?/e', "((substr('\\0',-1) == ';') ? '\\0' : '&amp;'.substr('\\0',1))", $s);
1337 at netapp dot com
1 year ago
If you need to process the content of your broken XML-doc you might find this interesting. It has blown past a few simple corruptions for me.
To Top