简体   繁体   English

使用PHP解析多个xml文件

[英]Using PHP to parse multiple xml files

I'm working on Mac and using Code Runner to do my PHP coding. 我正在Mac上工作,并使用Code Runner进行PHP编码。 I need to load multiple XMLs simultaneously with these: 我需要同时加载多个XML:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
    /*
    //to convert $a into $file after code is working proper
    $type = 'syll';
    $level = 'P1';
    $file = 'xml_'.$level.'_'.$type.'/';
    */
    $a = glob('xml_P1_syll/*.xml');
    //echo $file.'<br>';
    //print_r($a);
    foreach ($a as $i) {
        $xml = simplexml_load_file("$i")or die("Error: Cannot load file");

            foreach ($xml->children() as $entry => $data) {
                echo $data->section.'<br>';
                echo $data->source.'<br>';
                echo $data->question.'<br>';
                echo $data->instruction.'<br>';
            }
    }

?>

It worked successfully when i'm running from code runner itself… But when i upload to server it returned this error: 当我从代码运行器本身运行时,它成功运行。但是,当我上传到服务器时,它返回此错误:

Warning: Invalid argument supplied for foreach() in file.php on line 12

I've only started using foreach() since all the tutorials for php parsing xml strongly recommends using it. 我只是开始使用foreach(),因为所有用于php解析xml的教程都强烈建议使用它。 I've been using only for() all this while. 我一直都只使用for()。 Could it be syntax for foreach()? 可能是foreach()的语法吗? Or is it with php version (since it worked in code runner). 还是php版本(因为它在代码运行程序中起作用)。 Any pointers is greatly appreciated…Thanks 任何指针都将不胜感激...谢谢

can't comment as I don't have a rep of 50 yet, I suggest checking your directory reference to be correct xml_P1_syll/*.xml assuming /var/www or what have you as your base. 无法发表评论,因为我还没有代表50,建议您以/ var / www或以您为基础,检查目录引用是否正确xml_P1_syll / *。xml。 Check the DocumentRoot setting. 检查DocumentRoot设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM