【发布时间】:2013-01-23 07:14:58
【问题描述】:
我想使用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 警告:无法加载外部 实体 ”。”在第 22 行的 C:\wamp\www\readfiles.php 中,致命错误:调用 到非对象上的成员函数 xpath() C:\wamp\www\readfiles.php 第 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])."')");}}'