Fonction PHP libxml_get_errors ()

Définition et utilisation

XML est un langage de balisage pour partager les données sur le Web, XML est à la fois lisible par l'homme et par la machine. La classe libXMLError contient les erreurs générées par la bibliothèque libxml.

le libxml_get_errors() La fonction est utilisée pour récupérer les erreurs dans une chaîne ou un document XML.

Syntaxe

SimpleXMLElement:: libxml_get_errors();

Paramètres

Cette fonction n'accepte aucun paramètre.

Valeurs de retour

Cette fonction renvoie un tableau d'objets de type LibXMLError, chaque objet représentant un tableau dans le fichier / chaîne XML donné.

S'il n'y a pas d'erreur dans le XML spécifié, cette fonction renvoie une chaîne vide.

Version PHP

Cette fonction a été introduite pour la première fois dans la version 5 de PHP et fonctionne dans toutes les versions ultérieures.

Exemple

L'exemple suivant montre l'utilisation de la fonction libxml_get_errors ().

<html>
   <head>
      <body>
         <?php
            libxml_use_internal_errors(true);
            $str = "<Data xmlns:ns='http://test.com/data'> 
            <Employee> 
               <ns:Name>Krishna</ns:Name> 
               <Age>30</Age> 
               <City>Hyderabad</City> 
            </Employee> 
     
            <Employee> 
               <ns:Name>Ramu</ns:Name>
               <Age>25</Age> 
               <City>Delhi</test> 
            </Employee>    
            </Data> "; 
            $doc = simplexml_load_string($str);

            if ($doc === false) {
               $errors = libxml_get_errors();	
               print("Errors: ");			
               print_r($errors);
               echo "<br><br>";
            }
         ?>      
      </body>
   </head>   
</html>

Cela produira le résultat suivant -

Errors: Array ( 
   [0] => LibXMLError Object (
      [level] => 3 [code] => 76 
      [column] => 30 
      [message] => Opening and ending tag mismatch: Employee line 2 and Employee 
      [file] => 
      [line] => 6 
   ) 
   [1] => LibXMLError Object ( 
      [level] => 3 [code] => 76 
      [column] => 31 
      [message] => Opening and ending tag mismatch: City line 2 and test 
      [file] => [line] => 11 
   ) 
)

Exemple

Voici un exemple de cette fonction -

data.xml:

<Tutorials>
   <Tutorial>
      <Name>JavaFX</Name>
      <Pages>535</Pages>
      <Author>Krishna</Author>
      <Version>11<Version>
   </Tutorial>

   <Tutorial>
      <Name>CoffeeScript</Name>
      <Pages>235</Pages>
      <Author>Kasyap</test>
      <Version>2.5.1</Version>
   </Tutorial>
   
   <Tutorial>
      <Name>OpenCV</Name>
      <Pages>150</Pages>
      <Author>Maruti</Author>
      <Version></Version>
   </Tutorial>
</Tutorials>

Sample.html

<html>
   <head>      
      <body>         
         <?php
            libxml_use_internal_errors(true);
            $xml = simplexml_load_file("data.xml");
            if ($xml === false) {
               $errors = libxml_get_errors();	
               print("Errors: ");			
               foreach($errors as $ele) {        
                  print_r($ele);
                  echo "<br><br>";
               }	
            }
         ?>
      </body>
   </head>
</html>

Cela produira la sortie suivante -

Errors: LibXMLError Object ( 
   [level] => 3 
   [code] => 76 
   [column] => 15 
   [message] => Opening and ending tag mismatch: Version line 7 and Tutorial [file] => trail.xml 
   [line] => 8 
)
LibXMLError Object ( 
   [level] => 3 
   [code] => 76 
   [column] => 28 
   [message] => Opening and ending tag mismatch: Author line 7 and test 
   [file] => trail.xml [line] => 13 
)
LibXMLError Object ( 
   [level] => 3 
   [code] => 76 
   [column] => 13 
   [message] => Opening and ending tag mismatch: Version line 7 and Tutorials 
   [file] => trail.xml 
   [line] => 23 
)
LibXMLError Object ( 
   [level] => 3 
   [code] => 74 
   [column] => 13 
   [message] => EndTag: ' trail.xml [line] => 23 
)