我想使用xpath
方法将多个XML文件中的多个节点值插入MySQL。代码如下:
<?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])."')" );
}
}
?>
但这给了我以下错误:
警告:simplexml_load_file(.) [function.simplexml-load-file]: 无法打开流:在 C:\wamp\www\readfiles 中拒绝权限.php 在第 22 行,警告:simplexml_load_file() [function.simplexml-load-file]:I/O 警告:无法加载外部 C:\wamp\www\readfiles.php 第 22 行中的实体 "." 中,致命错误:调用 到非对象上的成员函数 xpath() C:\wamp\www\readfiles.php 在第 23 行。
请提出一些方法来解决这个问题。
readdir()
也返回".."和"..",这是错误消息试图告诉您的内容:
警告:simplexml_load_file(.) [function.simplexml-load-file]:失败 打开流:在 C:\wamp\www\readfiles.php 中拒绝权限 22, 警告: simplexml_load_file() [function.simplexml-load-file]: I/O 警告:无法加载外部实体"." C:\wamp\www\readfiles.php 在第 22 行,致命错误:呼叫成员 函数 xpath() 在 C:\wamp\www\readfiles.php 中的非对象上 在线 23
您可以改用glob
:
foreach(glob($path . "/*.xml") as $file)