• PHP Video Tutorials

PHP libxml_clear_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_clear_errors()

function is used to retrieve the last error in a XML string or document.

Syntax

libxml_clear_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 −

<?xml version="1.0" encoding="utf-8"?>
<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.xml:

<html>
   <head>
      <body>
         <?php
            libxml_use_internal_errors(true);
            $doc = new DOMDocument;
            $str = $doc->load(data.xml');
            if (!$str) {
               foreach (libxml_get_errors() as $error) {
                  print_r($error);
                  print("<br><br>");
               }
               libxml_clear_errors();
            }
         ?>      
      </body>
   </head>   
</html>

This will produce following result −

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