简体   繁体   中英

Using PHP to parse multiple xml files

I'm working on Mac and using Code Runner to do my PHP coding. I need to load multiple XMLs simultaneously with these:

<?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. I've been using only for() all this while. Could it be syntax for foreach()? Or is it with php version (since it worked in code runner). 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. Check the DocumentRoot setting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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