试图将XML文件解析为PHP中的CSV时的致命错误

发布于 2025-01-29 09:58:05 字数 1902 浏览 2 评论 0原文

我被困在程序上的一些代码上,我试图使用PHP中的函数将XML文档转换为CSV。该函数的代码是:

function createCsv($xml, $f)
    {
        foreach ($xml->children() as $item) 
        {
            $hasChild = (count($item->children()) > 0) ? true : false;
            if (!$hasChild) 
            {
                $put_arr = array($item->getName(), $item);
                fputcsv($f, $put_arr, ',', '"');
            } 
            else 
            {
                createCsv($item, $f);
            }
        }
    }

我在此处的主脚本中调用它:

if (file_exists($FilePath))
        {    
            echo "Converting, please stand by /n";

            $xml = $_FILES;
            $f = fopen('.csv', 'w');
            createCsv($xml, $f);
            fclose($f);
            //calling function to convert the xml file to csv

            $UploadDirectory = $UploadDirectory . basename($_FILES["fileToUpload"]["tmp_name"]);
            if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $UploadDirectory))
            {
                echo "The file has been uploaded and converted. Please click the link below to download it";
                echo '<a href="'.$UploadDirectory.'">'.$File.'</a>';
                //giving link to click and download converted CSV file
            }
            else
            {
                echo "There was a problem uploading and converting the file. Please refresh the page and try again.";
            }
        }

通过XAMPP运行脚本时收到的错误消息是:

Fatal error: Uncaught Error: Call to a member function children() on array in C:\xampp\htdocs\XMLtoCSV\convert.php:4 Stack trace: #0 C:\xampp\htdocs\XMLtoCSV\convert.php(73): createCsv(Array, Resource id #3) #1 {main} thrown in C:\xampp\htdocs\XMLtoCSV\convert.php on line 4

第4行引用是createCSV函数中的foreach语句。我真的很茫然,对PHP非常新。我不得不教自己的PHP,结果不同,任何帮助都将不胜感激。

I am stuck on a bit of code for my program, where I am attempting to convert a XML document to CSV using a function in PHP. The code for the function is:

function createCsv($xml, $f)
    {
        foreach ($xml->children() as $item) 
        {
            $hasChild = (count($item->children()) > 0) ? true : false;
            if (!$hasChild) 
            {
                $put_arr = array($item->getName(), $item);
                fputcsv($f, $put_arr, ',', '"');
            } 
            else 
            {
                createCsv($item, $f);
            }
        }
    }

And I am calling it in the main script here:

if (file_exists($FilePath))
        {    
            echo "Converting, please stand by /n";

            $xml = $_FILES;
            $f = fopen('.csv', 'w');
            createCsv($xml, $f);
            fclose($f);
            //calling function to convert the xml file to csv

            $UploadDirectory = $UploadDirectory . basename($_FILES["fileToUpload"]["tmp_name"]);
            if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $UploadDirectory))
            {
                echo "The file has been uploaded and converted. Please click the link below to download it";
                echo '<a href="'.$UploadDirectory.'">'.$File.'</a>';
                //giving link to click and download converted CSV file
            }
            else
            {
                echo "There was a problem uploading and converting the file. Please refresh the page and try again.";
            }
        }

the error message I get when running the script through XAMPP is:

Fatal error: Uncaught Error: Call to a member function children() on array in C:\xampp\htdocs\XMLtoCSV\convert.php:4 Stack trace: #0 C:\xampp\htdocs\XMLtoCSV\convert.php(73): createCsv(Array, Resource id #3) #1 {main} thrown in C:\xampp\htdocs\XMLtoCSV\convert.php on line 4

Line 4 that it is referencing is the foreach statement in the createCSV function. I am really at a loss, and very new to PHP. I have had to teach myself PHP with mixed results, and any assistance would be highly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉生梦死 2025-02-05 09:58:05

您将$ _文件作为XML文件,这是不正确的。
$ _文件是上传文件的关联数组。您需要打开文件并读取数据。为此,您可以使用Simplexml_load_file

$xml = simplexml_load_file($_FILES["fileToUpload"]["tmp_name"]);
createCsv($xml, $f);

You are considering $_FILES as the xml file, which is incorrect.
$_FILES is an associative array of uploaded files. You need to open the file and read the data. To do so you can use simplexml_load_file:

$xml = simplexml_load_file($_FILES["fileToUpload"]["tmp_name"]);
createCsv($xml, $f);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文