note 55699 added to ref.simplexml

From: Date: Wed, 10 Aug 2005 20:22:31 +0000
Subject: note 55699 added to ref.simplexml
Groups: php.notes 
Request: Send a blank email to php-notes+get-93397@lists.php.net to get a copy of this message
If submitting an erroneous XML file to simplXML it produces a flurry of PHP error messages which are not easy to interpret. The following function will return the simpleXML tree object for well formed XML and an readable error report for malformed XML. In the report, errors are edited and interspersed with the source XML listing. function validate_xml ( // Returns the simpleXML object for well formed XML and an error string for malformed ones. $xml, // The name of an XML file or an XML string $fullsrc = false) { // Flag indicating if the error report should list the full XML or just the incorrect lines // If the $xml parameter is a file name, read the file contents if ((strlen ($xml) < 256) && is_file ($xml)) $xml = file_get_contents ($xml); // Set up an error capture environment global $errors; // array of error messages $errors = array (); // This error handler will scan error messages generated by simpleXML for line references. The error message is added to the $errors array using these line numbers as keys. set_error_handler ( create_function ('$errno, $errstr', 'global $errors; if (preg_match_all ("!line ([0-9]+)!i", $errstr, $matches)) foreach ($matches[1] as $lno) $errors[$lno] = $errno." : ".$errstr;')); if (false === ($transfer = simplexml_load_string ($xml))) {// If errors occurred during the parse : // We wish to generate a listing, with line numbers, of the source XML displaying error messages after the lines in question. The whole to be printable and bracketed by <pre></pre> $i = 0; // line number // The following comments should be read from the innermost to the outermost $transfer = '<pre>'. // Finally replace the markers with proper html span tags. str_replace (array('#O#', '#F#'), array ('</pre><span style="color:red;">', '</span><pre>'), // Convert all special HTML characters htmlentities ( // preg_replace each complete line with a new string comprising of a line number, the line text, and any errors recorded for this line in the $errors array. In the case of $fullsrc being false, any lines without errors are eliminated. The error message texts are surrounded by marker flags which are replaced above. preg_replace ('!^(.*)$\n!em', '(isset ($errors[++$i]) || $fullsrc) ? sprintf ("\n[%4d] %s %s", $i, trim(\'$1\'), (isset($errors[$i]) ? "#O#${errors[$i]}#F#" : "")) : ""', $xml. "\n"))). '</pre>'; } // All done, $transfer contains either the simpleXML tree object or the error report, before returning it we need to tidy up: restore_error_handler (); unset ($errors); return $transfer; } A sample abbreviated report from an XML where open and close tag names do not correspond: [ 17] <QObjet boucle=\"Objets\" 2 : Entity: line 125: parser error : Opening and ending tag mismatch: QObjet line 17 and Objet [ 125] </Objet> 2 : Entity: line 125: parser error : Opening and ending tag mismatch: QObjet line 17 and Objet ---- Manual Page -- http://www.php.net/manual/en/ref.simplexml.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+55699 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55699&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55699&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55699&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55699&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55699&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55699&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+55699&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#93397) next »