Foros del Web » Programando para Internet » PHP »

[SOLUCIONADO] Leer texto row por row.

Estas en el tema de Leer texto row por row. en el foro de PHP en Foros del Web. hola, Tengo un codigo que lee un csv. y quiero sustituir unos atrributos de un xml especificos. El problema es que cada Row del csv. ...
  #1 (permalink)  
Antiguo 07/03/2014, 15:56
 
Fecha de Ingreso: diciembre-2005
Ubicación: Jalisco
Mensajes: 31
Antigüedad: 18 años, 4 meses
Puntos: 0
Leer texto row por row.

hola,
Tengo un codigo que lee un csv. y quiero sustituir unos atrributos de un xml especificos.
El problema es que cada Row del csv.
Código:
1234,5678,0987,6564 <- Row 1
1234,5678,0987,6564 <- Row2
Cada vez que termine de leer el primer Row, se cree un xml.
y cuando termine el segundo se cree otro xml...
y asi hasta que termine de leer todos.

este es mi codigo.

Código PHP:
<?php

function csv_in_array($url,$delm=",",$encl="\"") {

$doc = new DOMDocument();
$xml2 "A2CBOT.xml";
$doc->load($xml2);  
$xp = new DOMXPath($doc);

    
$csvxrow file($url);   // ---- csv rows to array ----
    
    
$csvxrow[0] = chop($csvxrow[0]);
    
$csvxrow[0] = str_replace($encl,'',$csvxrow[0]);
    
$keydata explode($delm,$csvxrow[0]);
    
    
$anzdata count($csvxrow);
    
$z=0;
    for(
$x=1$x<$anzdata$x++) {
        
$csvxrow[$x] = chop($csvxrow[$x]);
        
$csvxrow[$x] = str_replace($encl,'',$csvxrow[$x]);
        
$csv_data[$x] = explode($delm,$csvxrow[$x]);
        
$i=0;
        
$a=0;
        foreach(
$keydata as $key) {
            
$out[$z][$key] = $csv_data[$x][$i];
            
$i++;
            
////// Aqui reemplazara los atributos 

               
foreach($xp->query('/ROOT/HEADER/@KEY[. != ""]') as $attrib)
               {
                
$attrib->nodeValue $out[$z][$key];
               }
               foreach(
$xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1FKOL/@AUFNR[. != ""]') as $attrib)
               {
                
$attrib->nodeValue $out[$z][$key];
               }
               foreach(
$xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1FKOL/@GAMNG[. != ""]') as $attrib)
               {
                
$attrib->nodeValue $out[$z][$key];
               }
               foreach(
$xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1FKOL/@GSTRS[. != ""]') as $attrib)
               {
                
$attrib->nodeValue $out[$z][$key];
               }
               foreach(
$xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1FKOL/@GLTRS[. != ""]') as $attrib)
               {
                    
                   
//$datesum = date($out[$z][$key],  strtotime('+1 month'));
                
$attrib->nodeValue $datesum;
               }
               foreach(
$xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1AFFLL/E1FVOL/@MGVRG[. != ""]') as $attrib)
               { 
                 
$attrib->nodeValue $out[$z][$key];
                 }
            
//////

            
}
        
$z++;
        }
        
        
   echo 
$doc->saveXML(); 

return 
$out;
}

// --------------------------------------------------------------

?>

<?php

$csvdata 
csv_in_array("LISTOTA.csv"",""\"");
No encuentro la forma espero puedan ayudarme.

Gracias.
  #2 (permalink)  
Antiguo 09/03/2014, 13:27
Avatar de geq
geq
 
Fecha de Ingreso: agosto-2006
Ubicación: Rosario
Mensajes: 655
Antigüedad: 17 años, 8 meses
Puntos: 22
Respuesta: Leer texto row por row.

Hola, file() te devuelve un array en el que cada elemento es una línea del archivo. Así que a continuación de eso lo que tendrías que hacer es iterar $csvxrow. Ejemplo:

Código PHP:
Ver original
  1. <?php
  2.  
  3. function csv_in_array($url,$delm=",",$encl="\"") {
  4.  
  5. $doc = new DOMDocument();
  6. $xml2 = "A2CBOT.xml";
  7. $doc->load($xml2);  
  8. $xp = new DOMXPath($doc);
  9.  
  10.     $csvxrows = file($url);   // ---- csv rows to array ----
  11.     foreach($csvrows as $csvrow) {
  12.      
  13.         $csvxrow[0] = chop($csvxrow[0]);
  14.         $csvxrow[0] = str_replace($encl,'',$csvxrow[0]);
  15.         $keydata = explode($delm,$csvxrow[0]);
  16.      
  17.         $anzdata = count($csvxrow);
  18.         $z=0;
  19.         for($x=1; $x<$anzdata; $x++) {
  20.             $csvxrow[$x] = chop($csvxrow[$x]);
  21.             $csvxrow[$x] = str_replace($encl,'',$csvxrow[$x]);
  22.             $csv_data[$x] = explode($delm,$csvxrow[$x]);
  23.             $i=0;
  24.             $a=0;
  25.             foreach($keydata as $key) {
  26.                 $out[$z][$key] = $csv_data[$x][$i];
  27.                 $i++;
  28.                 ////// Aqui reemplazara los atributos  
  29.    
  30.                    foreach($xp->query('/ROOT/HEADER/@KEY[. != ""]') as $attrib)
  31.                    {
  32.                     $attrib->nodeValue = $out[$z][$key];
  33.                    }
  34.                    foreach($xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1FKOL/@AUFNR[. != ""]') as $attrib)
  35.                    {
  36.                     $attrib->nodeValue = $out[$z][$key];
  37.                    }
  38.                    foreach($xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1FKOL/@GAMNG[. != ""]') as $attrib)
  39.                    {
  40.                     $attrib->nodeValue = $out[$z][$key];
  41.                    }
  42.                    foreach($xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1FKOL/@GSTRS[. != ""]') as $attrib)
  43.                    {
  44.                     $attrib->nodeValue = $out[$z][$key];
  45.                    }
  46.                    foreach($xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1FKOL/@GLTRS[. != ""]') as $attrib)
  47.                    {
  48.                          
  49.                        //$datesum = date($out[$z][$key],  strtotime('+1 month'));
  50.                     $attrib->nodeValue = $datesum;
  51.                    }
  52.                    foreach($xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1AFFLL/E1FVOL/@MGVRG[. != ""]') as $attrib)
  53.                    {  
  54.                      $attrib->nodeValue = $out[$z][$key];
  55.                      }
  56.                 //////
  57.  
  58.                 }
  59.             $z++;
  60.             }
  61.     }    
  62.          
  63.    echo $doc->saveXML();  
  64.  
  65. return $out;
  66. }
  67.  
  68. // --------------------------------------------------------------
  69.  
  70. ?>
  71.  
  72. <?php
  73.  
  74. $csvdata = csv_in_array("LISTOTA.csv", ",", "\"");

Saludos.
  #3 (permalink)  
Antiguo 10/03/2014, 08:34
 
Fecha de Ingreso: diciembre-2005
Ubicación: Jalisco
Mensajes: 31
Antigüedad: 18 años, 4 meses
Puntos: 0
Respuesta: Leer texto row por row.

Hola muchas gracias... apenas vi tu mensaje y me encontre con fgetcsv();

que lee cada row, y creo es mucho mas sencillo de usar...

modifique el codigo que tenia por este.

EDIT: Para quien necesite o le sirva.

Este codigo lee un archivo csv y cada row edita atributos especificos de una plantilla xml.
y guarda un xml por cada row.

Muchas gracias por la ayuda.

Código PHP:
<?php

function csv_in_array($csv,$xml ) {

$doc = new DOMDocument();
$xml2 $xml;
$doc->load($xml2);  
$xp = new DOMXPath($doc);

   
$row 1;
   
$handle fopen($csv"r");
     
# Rows Counter
    
$csvxrow file($csv);
    
$csvxrow[0] = chop($csvxrow[0]);
    
$anzdata count($csvxrow);
   while ((
$data fgetcsv($handle1000",")) !== FALSE) {
       
$num count($data);
       
$row++;
         for (
$c=0$c $num$c++) {
       
               foreach(
$xp->query('/ROOT/HEADER/@KEY[. != ""]') as $attrib)
               {
                
$attrib->nodeValue $data[0];
               }
               foreach(
$xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1FKOL/@AUFNR[. != ""]') as $attrib)
               {
                
$attrib->nodeValue $data[0];
               }
               foreach(
$xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1FKOL/@MATNR[. != ""]') as $attrib)
               {
                
$attrib->nodeValue $data[1];
               }
               foreach(
$xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1FKOL/@GAMNG[. != ""]') as $attrib)
               {
                
$attrib->nodeValue $data[2];
               }
               foreach(
$xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1FKOL/@GSTRS[. != ""]') as $attrib)
               {
                
$attrib->nodeValue $data[3];
               }
               foreach(
$xp->query('/ROOT/DATA/SAPMES/LOIPRO/E1FKOL/@GLTRS[. != ""]') as $attrib)
               {
                 
$fecha = new DateTime($data[3]);
                  
$fecha->add(new DateInterval('P1M'));
                 
$attrib->nodeValue $fecha->format('Y-m-d');
               }
            }
               
$name $data[0] .'-'$data[1];
               
$ruta './XML/';
               
$doc->formatOutput true;
          echo 
$doc->saveXML();
               
$doc->save($ruta.date('Y-m-d').'-'.$name.'.xml');
       
    }
fclose($handle);

    echo 
$anzdata " XML Creados";  

return 
$data;
}
// --------------------------------------------------------------

?>

<?php

// ----- call ------
$csvdata csv_in_array("LISTOTA.csv""A2CBOT.xml");
// -----------------

?>
EDIT: Terminado y Funcionando.
Ya lo unico que me falta es crear un xml por cada row.

muchas gracias por la ayuda y saludos.

Última edición por striderwar; 10/03/2014 a las 10:50

Etiquetas: row
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 19:07.