Advertisement
Slyfer

XML+PHP

Jun 23rd, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.14 KB | None | 0 0
  1. <!-------------------XML------------------------------------------------------------------------------------>
  2. <?xml version="1.0" encoding="UTF-8"?>
  3.   <nfes>
  4.     <nfe>
  5.         <numero>269971</numero>
  6.         <xnome>Jose</xnome>
  7.         <cnpj>7157</cnpj>
  8.         <demi>2015</demi>
  9.         <icmstot>800</icmstot>
  10.         <destcnpj>85364</destcnpj>
  11.         <razao>ABC System</razao>
  12.     </nfe>
  13.     <nfe>
  14.         <numero>252694</numero>
  15.         <xnome>Jose</xnome>
  16.         <cnpj>7157</cnpj>
  17.         <demi>2015</demi>
  18.         <icmstot>800</icmstot>
  19.         <destcnpj>85364</destcnpj>
  20.         <razao>ABC System</razao>
  21.     </nfe>
  22.   </nfes>
  23.  
  24.  
  25. <!------------------------------------------------Arquivo para mostrar os dados em tela e salvar no banco ------------------------->
  26.  
  27. <?php
  28.  
  29. $xmlDoc = new DOMDocument();
  30. $xmlDoc->load("arquivo.xml");
  31. $mysql_hostname = "localhost";
  32. $mysql_user = "root";
  33. $mysql_password = "";
  34. $mysql_database = "trabalhoxml";
  35.  
  36. $dbh = new PDO("mysql:dbname={$mysql_database};host={$mysql_hostname};port=3306", $mysql_user, $mysql_password);
  37.  
  38. $xmlObject = $xmlDoc->getElementsByTagName('nfe');
  39. $itemCount = $xmlObject->length;
  40.  
  41. for ($i=0; $i < $itemCount; $i++){
  42.    $numero = $xmlObject->item($i)->getElementsByTagName('numero')->item(0)->childNodes->item(0)->nodeValue;
  43.    $xnome = $xmlObject->item($i)->getElementsByTagName('xnome')->item(0)->childNodes->item(0)->nodeValue;
  44.    $cnpj = $xmlObject->item($i)->getElementsByTagName('cnpj')->item(0)->childNodes->item(0)->nodeValue;
  45.    $demi = $xmlObject->item($i)->getElementsByTagName('demi')->item(0)->childNodes->item(0)->nodeValue;
  46.    $icmstot = $xmlObject->item($i)->getElementsByTagName('icmstot')->item(0)->childNodes->item(0)->nodeValue;
  47.    $destcnpj = $xmlObject->item($i)->getElementsByTagName('destcnpj')->item(0)->childNodes->item(0)->nodeValue;
  48.    $razao = $xmlObject->item($i)->getElementsByTagName('razao')->item(0)->childNodes->item(0)->nodeValue;
  49.    
  50.    
  51.    $sql = $dbh->prepare("INSERT INTO 'tabela'(numero, 'xnome', cnpj, demi, icmstot, destcnpj, 'razao') VALUES (?, ?, ?, ?, ?, ?, ?)");
  52.    $sql->execute(array($numero,$xnome,$cnpj,$demi,$icmstot,$destcnpj,$razao));
  53.    print "$numero.$xnome.$cnpj.$demi.$icmstot.$destcnpj.$razao<br/>";
  54. }
  55.  
  56. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement