2013-01-23 90 views
-2

我想使用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.

请建议一些方法来处理这个问题。

+0

请采取格式化你的代码,使其可读的时间。 –

+0

我曾尝试用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])。'')“);}}' –

回答

0

readdir()也返回“。”。和“..”,这是错误消息试图告诉你有:

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代替:

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

感谢您的建议,我尝试过使用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”); \t $ products [0] =(string)current($ xml-> xpath(“/ sioct:BoardPost/sioc:content”)); mysql_query(“INSERT INTO forum(txt_content,txt_date,txt_about)VALUE(''。mysql_escape_string($ products [0])。”','“。mysql_escape_string($ products [1])。”','“。mysql_escape_string( $产品[2]) “')”)。 } –

+0

我说glob **而不是readdir,不要把它们放在一起,直到没有任何意义了...请查阅,[glob](http://php.net/glob)是如何工作的。 –

+0

谢谢,我尝试过使用glob(),但它再次没有向数据库插入任何数据,我想我正在做一些基本的错误,请指导我处理它:'if($ handle = opendir($ path)) { ($($ path =“/*.xml”)作为$ file)foreach($(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($产品[2]) “')”)。 } } –