PHP libxml_get_errors() Function



Definition and Usage

XML is a mark-up language to share the data across the web, XML is for both human read-able and machine read-able. The libXMLError class contains the errors thrown by the libxml library.

The libxml_get_errors() function is used to retrieve the errors in a XML string or document.

Syntax

SimpleXMLElement:: libxml_get_errors();

Parameters

This function does not accept any parameters.

Return Values

This function returns an array of objects of the type LibXMLError, each object representing an array in the given XML file/string.

If there are no errors in the specified XML this function returns an empty string.

PHP Version

This function was first introduced in PHP Version 5 and works in all the later versions.

Example

Following example demonstrates the usage of the libxml_get_errors() function.


   
      
          
             
               Krishna 
               30 
               Hyderabad 
             
     
             
               Ramu
               25 
               Delhi 
                
             "; 
            $doc = simplexml_load_string($str);

            if ($doc === false) {
               $errors = libxml_get_errors();	
               print("Errors: ");			
               print_r($errors);
               echo "

"; } ?>

This will produce following result −

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

Example

Following is an example of this function −

data.xml:


   
      JavaFX
      535
      Krishna
      11
   

   
      CoffeeScript
      235
      Kasyap
      2.5.1
   
   
   
      OpenCV
      150
      Maruti
      
   

Sample.html


         
               
         
"; } } ?>

This will produce the following output −

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 
)
php_function_reference.htm
Advertisements