Jump to content
php.lv forumi

Recommended Posts

Posted (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 by Bunkertor 7
Posted

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
}

Posted

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.

Posted (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 by blackhalt

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...