• PHP Video Tutorials

PHP libxml_get_last_error() 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_last_error() function is used to retrieve the last error 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 object of the type LibXMLError representing the last error in the 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_last_error() function.

<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> 
               </Employeee> 
        
               <Employee> 
                  <ns:Name>Ramu</ns:Name>
                  <Age>25</Age> 
                  <City>Delhi</test> 
               </Employee>    
            </Data> "; 
            $doc = simplexml_load_string($str);

            if ($doc === false) {
               $error = libxml_get_last_error();	
               print("Error: ");			
               print_r($error);
            }
         ?>      
      </body>
   </head>   
</html>

This will produce following result −

Error: 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 another example of this function −

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) {
               $error = libxml_get_last_error();	
               print("Error: ");			
               print_r($error);
               echo "<br><br>";         
            }
         ?>
      </body>
   </head>
</html>

This will produce the following output −

Error: LibXMLError Object ( 
   [level] => 3 
   [code] => 74 
   [column] => 13 
   [message] => EndTag: ' trail.xml 
   [line] => 23 
)
php_function_reference.htm
Advertisements