我想使用xpath
方法将多个XML文件中的多个节点值插入到MySQL中。代码如下:使用XPath处理多个XML文件
<?php
$path="fs/";
$con = mysql_connect("localhost","sufi","1234");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
if ($handle = opendir($path)) {
$files = array();
while (($file = readdir($handle)) !== false) {
$files[] = $file;
}
sort($files);
foreach ($files as $file) {
$xml = simplexml_load_file("$file");
$products[0] = (string)current($xml->xpath("/sioct:BoardPost/sioc:content"));
$products[1] = (string)current($xml->xpath("/sioct:BoardPost/dcterms:created"));
$products[2] = (string)current($xml->xpath("/sioct:BoardPost/@rdfabout"));
extract($products);
mysql_query("INSERT INTO forum (txt_content,txt_date,txt_about)
VALUES ('".mysql_escape_string($products[0])."',
'".mysql_escape_string($products[1])."',
'".mysql_escape_string($products[2])."')");
}
}
?>
但是这给了我以下错误:
Warning: simplexml_load_file(.) [function.simplexml-load-file]: failed to open stream: Permission denied in C:\wamp\www\readfiles.php on line 22, Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "." in C:\wamp\www\readfiles.php on line 22, Fatal error: Call to a member function xpath() on a non-object in C:\wamp\www\readfiles.php on line 23.
请建议一些方法来处理这个问题。
请采取格式化你的代码,使其可读的时间。 –
我曾尝试用glob()从目录中检索多个xml文件,并试图将每个文件的内容插入到mysql中,但它并没有向数据库插入任何数据,请检查我的代码并引导我: 'if($ handle = opendir($ path)){foreach(glob($ path。“/*.xml”)as $ file){$ xml = simplexml_load_file($ file); $ products [0] =(string)current($ xml-> xpath(“/ sioct:BoardPost/sioc:content”)); mysql_query(“INSERT INTO forum(txt_content,txt_date,txt_about)VALUES(''.mysql_escape_string($ products [0])。”','“。mysql_escape_string($ products [1])。”','“。 mysql_escape_string($ products [2])。'')“);}}' –