如何在PHP中将XML子节点值保存到数据库
我需要关于将XML数据保存到MySQL数据库的帮助。这是我的密码:如何在PHP中将XML子节点值保存到数据库,php,mysql,xml,Php,Mysql,Xml,我需要关于将XML数据保存到MySQL数据库的帮助。这是我的密码: <?xml version="1.0" encoding="UTF-8"?><Response Code="200"><Description>http://sample.net</Description><URL>/Patient/PatientView.aspx?pid=642</URL></Response> http://sam
<?xml version="1.0" encoding="UTF-8"?><Response Code="200"><Description>http://sample.net</Description><URL>/Patient/PatientView.aspx?pid=642</URL></Response>
http://sample.net/Patient/PatientView.aspx?pid=642
现在我想做的是获取
标记和
标记的值,并将它们组合成一个完整的url,然后将其保存到mysql数据库中。请参见此处的问题
您可以使用SimpleXML(请参阅)进行解析
$xmlStr = '<?xml version="1.0" encoding="UTF-8"?><Response Code="200"><Description>http://sample.net</Description><URL>/Patient/PatientView.aspx?pid=642</URL></Response>';
$response = new SimpleXMLElement($xmlStr);
$url = (string) $response->Description . (string) $response->URL;
然后使用PDO
()将数据存储到数据库中:
try {
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$stmt = $dbh->prepare("INSERT INTO sample (url) VALUES (:url)");
$stmt->bindParam(':url', $url);
$stmt->execute();
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
试试看{
$dbh=newpdo('mysql:host=localhost;dbname=test',$user,$pass);
$stmt=$dbh->prepare(“插入样本(url)值(:url)”);
$stmt->bindParam(':url',$url);
$stmt->execute();
}捕获(PDO$e){
打印“错误!:”$e->getMessage()。“
”;
模具();
}
您需要先解析xml,使用simplexml
完成解析后,再使用pdo连接并进行插入谢谢我试试您的建议,先生:-)谢谢@Sergey Kasatkin,我试试这个。:-)
try {
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$stmt = $dbh->prepare("INSERT INTO sample (url) VALUES (:url)");
$stmt->bindParam(':url', $url);
$stmt->execute();
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}