Jump to content
Fórum Script Brasil
  • 0

Problemas com DOMDocument e XML


Batz

Question

E ae pessoal!

Preciso que meu sistema leia arquivos xml e insira os dados do arquivos no banco de dados. Já tinha feito algo do tipo antes com o DOM e repeti dessa vez mas o código não funcionou, minha tabela fica lotada de espaços em branco =(

o código está assim:

$doc = new DOMDocument();
  $doc->load( $xml );
  
  $news = $doc->getElementsByTagName( "produto" );
  foreach( $news as $new )
  { 
      $i = 0;
    $mm = "";
    $act = "";
    $crom = "";
    $ant = "";
    $epox = "";
    $sa = "";
    $lp = "";
      $rows = $new->getElementsByTagName( "desc_produto" );
        foreach( $rows as $row ){ 
            $nomes = $row->getElementsByTagName( "prod" );
            $nome .= $nomes->item(0)->nodeValue;
      
            $dets = $row->getElementsByTagName( "det" );
            $det .= $dets->item(0)->nodeValue;
      
            $mms = $row->getElementsByTagName( "mm" );
            $mm[$i] = $mms->item(0)->nodeValue;
        
            $acts = $row->getElementsByTagName( "act" );
            $act[$i] = $acts->item(0)->nodeValue;
        
            $croms = $row->getElementsByTagName( "crom" );
            $crom[$i] = $croms->item(0)->nodeValue;
        
            $ants = $row->getElementsByTagName( "ant" );
            $ant[$i] = $ants->item(0)->nodeValue;
        
            $epoxs = $row->getElementsByTagName( "epox" );
            $epox[$i] = $epoxs->item(0)->nodeValue;
        
            $sas = $row->getElementsByTagName( "sa" );
            $sa[$i] = $sas->item(0)->nodeValue;
        
            $lps = $row->getElementsByTagName( "lp" );
            $lp[$i] = $lps->item(0)->nodeValue;
            $i++;
        }
    
  $imgs = $new->getElementsByTagName( "img" );
  $img = $imgs->item(0)->nodeValue;
  
  $prd1 = $new->getElementsByTagName( "prod1" );
  $prod1 = $prd1->item(0)->nodeValue;
  
  $prd2 = $new->getElementsByTagName( "prod2" );
  $prod2 = $prd2->item(0)->nodeValue;
  
  $prd3 = $new->getElementsByTagName( "prod3" );
  $prod3 = $prd3->item(0)->nodeValue;
  
  $prd4 = $new->getElementsByTagName( "prod4" );
  $prod4 = $prd4->item(0)->nodeValue;
  
  $prd5 = $new->getElementsByTagName( "prod5" );
  $prod5 = $prd5->item(0)->nodeValue;
  
  $prd6 = $new->getElementsByTagName( "prod6" );
  $prod6 = $prd6->item(0)->nodeValue;
  
  $prd7 = $new->getElementsByTagName( "prod7" );
  $prod7 = $prd7->item(0)->nodeValue;
  
  $prd8 = $new->getElementsByTagName( "prod8" );
  $prod8 = $prd8->item(0)->nodeValue;
  
  $prd9 = $new->getElementsByTagName( "prod9" );
  $prod9 = $prd9->item(0)->nodeValue;
  
  $prd10 = $new->getElementsByTagName( "prod10" );
  $prod10 = $prd10->item(0)->nodeValue;
  
  $prd11 = $new->getElementsByTagName( "prod11" );
  $prod11 = $prd11->item(0)->nodeValue;
  
  $prd12 = $new->getElementsByTagName( "prod12" );
  $prod12 = $prd12->item(0)->nodeValue;
  
  $prd13 = $new->getElementsByTagName( "prod13" );
  $prod13 = $prd13->item(0)->nodeValue;
  
  $selist = $new->getElementsByTagName( "sels" );
  $sels = $selist->item(0)->nodeValue;
  
  $mm = serialize($mm);
  $act = serialize($act);
  $crom = serialize($crom);
  $ant = serialize($ant);
  $epox = serialize($epox);
  $sa = serialize($sa);
  $lp = serialize($lp);
  if($prod1 == ""){$prod1='act';}
  if($prod2 == ""){$prod2='crom';}
  if($prod3 == ""){$prod3='ant';}
  if($prod4 == ""){$prod4='cract';}
  if($prod5 == ""){$prod5='crbco';}
  if($prod6 == ""){$prod6='crpto';}
  if($prod7 == ""){$prod7='sa';}
  if($prod8 == ""){$prod8='epx';}
  if($prod9 == ""){$prod9='lp';}
  if($prod10 == ""){$prod10='lact';}
  if($prod11 == ""){$prod11='lant';}
  if($prod12 == ""){$prod12='lcr';}
  if($prod13 == ""){$prod13='lcra';}
  $sql = mysql_query("INSERT INTO $tab(prod,det,mm,act,crom,ant,epx,sa,lp,img,prod1,prod2,prod3,prod4,prod5,prod6,prod7,prod8,prod9,prod10,prod11,prod12,prod13) VALUES ('$nome','$det','$mm','$act','$crom','$ant','$epox','$sa','$lp','$img', '$prod1', '$prod2', '$prod3', '$prod4', '$prod5', '$prod6', '$prod7', '$prod8', '$prod9', '$prod10', '$prod11', '$prod12', '$prod13')");
  $no++;
  }
  echo "<font color='#FFFFFF'>Importação realizada com sucesso! $no cadastros foram realizados!</font>";

o echo está retornando a inclusão de 159 cadastros (o xml tem 159 produtos) e o banco de dados recebe os 159 cadastros mas os campos prod(nome),det(detalhe) estão vazios e a array dos campos mm até lp estão com valores em branco também =(

SOMEBODY HELP ME!

Link to comment
Share on other sites

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

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

Guest
Answer this question...

×   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.



  • Forum Statistics

    • Total Topics
      152.2k
    • Total Posts
      652k
×
×
  • Create New...