2013-07-02 35 views
1

我應該讀取文件夾中的許多xml文件並從這些數據中提取出來。 我沒有問題,使用此代碼讀取該文件夾在單純的文件夾中讀取許多xml文件

<?php 
$dir = "Dati/xml/nonletti/"; 
    if (is_dir($dir)) { 
    if ($dh = opendir($dir)) { 
    while (($file = readdir($dh)) !== false) { 
     if (($file !== '.') && ($file !== '..')) { 
    echo "$file \n"; 
    } 
} 
    closedir($dh); 
} 
} 
?> 

,但如果我嘗試使用SimpleXML來讀取所有的文件我沒有看到任何東西

<?php 
$dir = "Dati/xml/nonletti/"; 
if (is_dir($dir)) { 
    if ($dh = opendir($dir)) { 
    while (($file = readdir($dh)) !== false) { 
     if (($file !== '.') && ($file !== '..')) { 
    $xml = simplexml_load_file($file); 
     $RGSostituzione = $xml->attributes()->Sostituzione; 
    echo "<li>File $file - <b>Sostituzione:</b> $RGSostituzione</li>"; 
    } 
    } 
    closedir($dh); 
    } 
} 
?> 

你能幫助我,告訴我怎麼做? thank's- Filippo

回答

0

當您讀取目錄中的文件時,您只會得到基文件名,而不是完整路徑。因此,當您執行SimpleXML調用時,您需要預先設置路徑。

更改爲:

$xml = simplexml_load_file($dir . $file); 
+0

謝謝!有用 –