【问题标题】:Handling multiple XML files using XPath使用 XPath 处理多个 XML 文件
【发布时间】: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])."')");}}'

标签: php mysql xml


【解决方案1】:

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 行,致命错误:调用成员 在线 C:\wamp\www\readfiles.php 中的非对象上的函数 xpath() 23

您可以改用glob

foreach(glob($path . "/*.xml") as $file)

【讨论】:

  • 感谢您的建议,我尝试使用 glob() 选项,但数据未填充到 mysql 中,应用程序也执行没有错误。 if ($handle = opendir($path)) {$files = array();}while (($file = readdir($handle))!== false){$files[]=$file;foreach(glob( $path . "/*.xml") as $file){$xml=simplexml_load_file("$files"); $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])."')"); }
  • 我说的是 glob 而不是 readdir,在没有任何意义之前不要把它们放在一起......请查一下,glob 是如何工作的。
  • 谢谢,我尝试使用 glob() 但它再次没有向数据库插入任何数据,我想我犯了一些基本错误,请指导我处理它:'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])."')"); } }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-22
  • 1970-01-01
  • 2020-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多