Bunkertor 7 Posted June 29, 2012 Report Share Posted June 29, 2012 (edited) Es mēģinu izprast Loop būtību. Piemēram, ir skripts, kas ievāc informāciju par XML failiem. simplexml_load_file funkcija nobeigsies ar Fatal Error katru reizi kad XML būs invalīds. Pieņemsim ka šis Loop apstrādās 10 XML failus, no kuriem sestais būs invalīds. Tātad skripts pie sestā vienkārši pārtrauks darboties, neskatoties uz to ka aiz tā vēl ir trīs labi XML faili, kurus varētu apstrādāt. Kāds ir vienkāršākais veids, lai šo problēmu atrisinātu? Ideāli, saskaroties ar Fatal Error, šis skripts saglabātu datubāzē kautkādu "dežūrvērtību" kā Error vai Invalid XML un automātiski atsāktu apstrādi pie septītā faila. while($procRow = mysql_fetch_array($toProcess)) { //sheit notiek xml apstraade, variabli utt $xml=simplexml_load_file($file); //apstraade beidzas //ievieto variablus datubaazee $sqlup="UPDATE table"; $resultup=mysql_query($sqlup); } Edited June 29, 2012 by Bunkertor 7 Quote Link to comment Share on other sites More sharing options...
daGrevis Posted June 29, 2012 Report Share Posted June 29, 2012 for: try { do_stuff(); } catch(Exception e) { // Jaukais feils! } endfor; Quote Link to comment Share on other sites More sharing options...
101111 Posted June 29, 2012 Report Share Posted June 29, 2012 cik man zināms simplexml_load_file neizmet exception, tapēc nekādus exception nenoķersi. vai mēģināju erroru suppresošanu ar @ operatoru? $xml = @simplexml_load_file($url); if ($xml === false ) { //kļūdains XML } Quote Link to comment Share on other sites More sharing options...
daGrevis Posted June 30, 2012 Report Share Posted June 30, 2012 Nē, bet mans piemērs bija domās vispārējs. :) > Use libxml_use_internal_errors() to suppress all XML errors, and libxml_get_errors() to iterate over them afterwards. Quote Link to comment Share on other sites More sharing options...
blackhalt Posted July 1, 2012 Report Share Posted July 1, 2012 (edited) <?php libxml_use_internal_errors(true); $sxe = simplexml_load_file("test.xml"); if (!$sxe) { echo "Failed loading XML\n"; foreach(libxml_get_errors() as $error) { echo "\t", $error->message; } } else{ print_r($sxe); } ?> http://lv.php.net/ma...ples-errors.php un tad kaut kā tā: <?php libxml_use_internal_errors(true); foreach (glob("*.xml") as $filename) { $sxe = simplexml_load_file($filename); if (!$sxe) { echo "Failed loading XML\n"; foreach(libxml_get_errors() as $error) { echo "\t", $error->message; } } else{ print_r($sxe); } } ?> Edited July 1, 2012 by blackhalt Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.