<?php
$xml = simplexml_load_file('books.xml');
// print_r($xml);
// echo "<br/>";
$t = $xml->to;
$f = $xml->from;
$h = $xml->heading;
$b = $xml->body;
$dsn = "mysql:host=localhost;dbname=demo";
$username = "root";
$password = "00000000";
$conn = new PDO($dsn,$username,$password);
try{
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("insert into xml(t,f,heading,body) values(:t,:f,:h,:b)");
$stmt->bindParam(':t',$t);
$stmt->bindParam(':f',$f);
$stmt->bindParam(':h',$h);
$stmt->bindParam(':b',$b);
$stmt->execute();
echo "success";
}catch(PDOException $e){
echo "Error" . $e->getMessage();
}
$conn = null;
xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>