Skip an iteration with Foreach when found error in file (PHP)

Asked

Viewed 189 times

1

I am working on a web system that reads XML files on localhost, reading and checks I am able to unroll quiet, only have a little problem some files comes with error, because when generated XML ends up being mounted empty, I am not able to make my Loop Foreach skip this type of file and go to the next one, like this file with error, Node Father is not found and with that the system for Foreach, I have been researching about CONTINUE but I had no success.inserir a descrição da imagem aqui

//Instanciando o OBJ
$xml = new DOMdocument();


//Pegando o TMP do FORM
$arquivo = $_FILES['entXML']['tmp_name'];

//Entra dentro do NÓ pai do XML
$arquivo[$i] = $xml->getElementsByTagName( "nfeProc" );

inserir a descrição da imagem aqui

for($i = 0; $i < count($arquivo); $i++) {
    $totArquivos +=count($arquivo);

    foreach( $arquivo as $arquivo[$i]) {//Inicia o Loop pela Variavel que recebe os arquivos. 


     $xml->load($arquivo[$i]) or die(" ");//Carregando o XML 

     $arquivo[$i] = $xml->getElementsByTagName( "nfeProc" );//Entra na TAG Pai do XML

    if(empty($arquivo[$i]) ){ continue 1; }//Aqui eu verifico se e vazio, mas ele não ta pulando a iteração, o log de erro acusa o arquivo como Vazio(Empty-File)


    }
}

Error file img and error free file when opened in browser

  • 1

    Well, the continue has everything to be the solution you seek. Could elaborate a [mcve] demonstrating what you tried to do and what was the output obtained?

  • for($i = 0; $i < Count($file);$i++ ){ $totArchives +=Count($file); foreach( $file as $file[$i]){ //Loading XML $xml ->load($file[$i]) or die(" "); $file[$i] = $xml->getelementsbytagname( "nfeProc" ); if(Empty($file[$i]) ){ continue 1; }

  • The parent tag and "nfeProc" I’m associating to a variable and then as ta ai on the last line I check if empty, right after I use (continue 1) to pass an iteration. and follow in the next XML.

  • 1

    Gabriel, can you [Edit] the question to add this and remember to properly format the code to facilitate reading.

  • What would that line be, foreach( $arquivo as $arquivo[$i])?

  • The problem and the total number of characters allowed, so I couldn’t even do the formatting properly...

  • This is where I run all the XML files that are in the variable file, files that come from a form before Foreach I run a FOR on the variable file to go through all the files.

  • I only find my question every time I come here. kkkk

Show 3 more comments

1 answer

0


I believe the problem is occurring on the line:

$arquivo[$i] = $xml->getElementsByTagName( "nfeProc" );//Entra na TAG Pai do XML

The purpose of the method DOMDocument::getElementsByTagName( string $name ) : DOMNodeList is to search an XML tree and return with a list DOMNodeList the elements at the local level are objects of the class DOMNode whose property [DOMNode::nodeName] is equal to the parameter $name. Or colloquially it scans the XML tree in the higher scope and returns the elements whose tags are $name. This method finding no match returns an object DOMNodeList emptiness. This implying that preposition empty($arquivo[$i]) on the line...

if(empty($arquivo[$i]) ){ continue 1; }

...will always be fake and never getting to the code of continue.

To check whether getElementsByTagName did not find any tag to be tested property DOMNodeList::length which returns the number of nodes in the list.

for($i = 0; $i < count($arquivo); $i++) {
    $totArquivos +=count($arquivo);

    foreach( $arquivo as $arquivo[$i]) {//Inicia o Loop pela Variavel que recebe os arquivos. 


       $xml->load($arquivo[$i]) or die(" ");//Carregando o XML 

       $arquivo[$i] = $xml->getElementsByTagName( "nfeProc" );//Entra na TAG Pai do XML

       // Verifica se a lista retornada por $xml->getElementsByTagName( "nfeProc" ) é ou não vazia
       if ($arquivo[$i]->length==0) { 
          continue 1; // Se a lista for vazia length==0
       } 


    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.