我需要有关将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>
现在我想要做的是获取<Description>
标记和<URL>
标记的值,并将它们组合成一个完整的url,然后将其保存到mysql数据库。
答案 0 :(得分:1)
在此处查看问题How do you parse and process HTML/XML in PHP?
您可以使用SimpleXML(请参阅http://php.net/manual/en/simplexml.examples-basic.php)进行解析
$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;
$url
将包含:
http://sample.net/Patient/PatientView.aspx?pid=642
然后使用PDO
(http://php.net/manual/en/book.pdo.php)将数据存储到数据库中:
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();
}