asimryu

xml-php-sample-2

Jul 27th, 2016
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.01 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="ko">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>php-xml</title>
  6. </head>
  7. <body>
  8.     <?php
  9.     $db = new PDO("mysql:host=localhost;dbname=web;charset=utf8","root","");
  10.     $db->query("truncate city");   
  11.     $xml = simplexml_load_file("./korea3.xml");
  12.     $citylist = $xml->city;
  13.     foreach($citylist as $city){
  14.         $cityattr = $city->attributes();
  15.         foreach($cityattr as $key=>$value){
  16.             $$key = $value;
  17.         }
  18.         $info = addslashes($city->info);
  19.         $area = $city->area;
  20.         $population = $city->population;
  21.         echo "<h1>{$id}){$name_ko}</h1>";
  22.         echo "<h2>{$name_en}</h2>";
  23.         echo "<h3>{$name_full}</h3>";
  24.         echo "<p>{$area}</p>";
  25.         echo "<p>{$population}</p>";
  26.         echo "<p>{$info}</p><hr>";
  27.         $sql = "insert into city set ";
  28.         $sql .= "cityid='{$id}'";
  29.         $sql .= ",name_ko='{$name_ko}'";
  30.         $sql .= ",name_en='{$name_en}'";
  31.         $sql .= ",name_full='{$name_full}'";
  32.         $sql .= ",area='{$area}'";
  33.         $sql .= ",population='{$population}'";
  34.         $sql .= ",info='{$info}'";
  35.         $db->query($sql);
  36.     }
  37.     ?>
  38. </body>
  39. </html>
Advertisement
Add Comment
Please, Sign In to add comment