dpn68721 2011-04-25 07:39
浏览 34
已采纳

Simplexml从数组加载文件

Aloha everyone,

I have another question. I have a SYDI script that retrieves WMI data from Windows computers, then appends a timestamp to the end of the file, and finally saves it to a share on a server. The PHP code I have to read and print out the filenames is shown below:

if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            if ($file != "." && $file != "..") {
                $results[] = $file;
                echo $file . "<br />";
            }
        }
        closedir($dh);      
    }
}

Everything is good and the filenames are returned as shown below:

sydiResult-24-Apr-11-18-59-52.xml
sydiResult-24-Apr-11-19-00-32.xml
sydiResult-24-Apr-11-19-01-17.xml

I've tried to use simplexml to call the files, in order, to see if I could even do it, with the following code:

for ($i = 0; $i < sizeof($results); $i++) {
    $xml = simplexml_load_file($results[$i]);
}

but I get errors indicating that simplexml couldn't open external xml files. The php file is in my htdocs directory, and the xml files are in a directory in the htdocs directory, called sydiResults. I wanted to separate the sydi xml files, so I decided to make the sub-directory. Could that be the reason simple xml won't work?

My plan is simple: open an the xml files and extract certain information from it, then loop to the next xml file, extract certain information from it, then ... so on and so on until I reach the last xml file in the directory. Does anyone have an idea of how I can use simplexml, or if I can use simplexml to open each xml file in turn?

Thanks in advance for any and all responses.

  • 写回答

2条回答 默认 最新

  • dounao1856 2011-04-25 08:45
    关注

    try this :

    for ($i = 0; $i < sizeof($results); $i++) {
    $file = 'sydiResults/'.$results[$i];
    if (file_exists($file)) {
       $xml = simplexml_load_file($file);
        print_r($xml);
     }
    else {
    exit('Failed to open '.$file);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错