Advertisement
william73

xml2

Dec 18th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.44 KB | None | 0 0
  1. <?php
  2. $xmlDoc = new DOMDocument();
  3. $xmlDoc->load('example.xml');
  4. $mysql_hostname = 'localhost'; // Example : localhost
  5. $mysql_user = 'alb88945_admin_1';
  6. $mysql_password = 'dEu)~XUAN3JW';
  7. $mysql_database = 'alb88945_mediaging';
  8.  
  9.  
  10.  
  11. $dbh = new PDO("mysql:dbname={$mysql_database};host={$mysql_hostname};port=3306", $mysql_user, $mysql_password);
  12.  
  13. $xmlObject = $xmlDoc->getElementsByTagName('annuncio');
  14. $itemCount = $xmlObject->length;
  15.  
  16. for ($i=0; $i < $itemCount; $i++){
  17.    $name = $xmlObject->item($i)->getElementsByTagName('id_annuncio_immobile')->item(0)->childNodes->item(0)->nodeValue;
  18.   //  $link = $xmlObject->item($i)->getElementsByTagName('url')->item(0)->childNodes->item(0)->nodeValue;
  19.    $sql = $dbh->prepare("INSERT INTO `my_table_name`(`title`) VALUES (?)");
  20.    $sql->execute(array(
  21.      $name,
  22.    //  $link
  23.    ));
  24.    
  25.    
  26.    print "Finished Item $name n<br/>";
  27.    
  28.    
  29.    
  30.                 // inizio secondo ciclo
  31.                
  32.                
  33.                    
  34.                     for ($i=0; $i < $itemCount; $i++){
  35.                     $link = $xmlObject->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
  36.                   //  $link = $xmlObject->item($i)->getElementsByTagName('url')->item(0)->childNodes->item(0)->nodeValue;
  37.                    $sql = $dbh->prepare("INSERT INTO `my_table_name_2`(`link`) VALUES (?)");
  38.                    $sql->execute(array(
  39.                      $link
  40.                    //  $link
  41.                    ));
  42.                
  43.                
  44.                     }
  45.                
  46.                 // fine secondo ciclo
  47.    
  48.    
  49.    
  50.    
  51.    
  52.    
  53.    
  54. }
  55. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement