【问题标题】:read many xml file in a folder whith simplexml [duplicate]使用simplexml读取文件夹中的许多xml文件[重复]
【发布时间】:2013-07-02 12:02:39
【问题描述】:

我是否应该读取文件夹中的许多 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);
  }
}
?>

你能帮我告诉我怎么做吗? 谢谢- 菲利波

【问题讨论】:

    标签: php xml simplexml opendir


    【解决方案1】:

    当您读取目录中的文件时,您只会获得基本文件名,而不是完整路径。因此,您需要在执行 SimpleXML 调用时预先添加路径。

    改为:

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

    【讨论】:

      【解决方案2】:
      $xml = simplexml_load_file($dir . $file);
      

      https://php.net/readdir(返回文件名,而不是整个文件路径)

      https://php.net/simplexml_load_file(获取完整文件路径)

      【讨论】:

        猜你喜欢
        • 2014-05-06
        • 1970-01-01
        • 1970-01-01
        • 2019-06-25
        • 1970-01-01
        • 2019-02-17
        • 2013-05-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多