首页 > 解决方案 > php - 无法从字符串解析 XML - Laravel 存储

问题描述

我正在为 Laravel 使用Orchestra/解析器。我已经安装了它,就像文档说的那样,我正在尝试使用它来读取我的服务器上可用的 XML 文件。我有一个 FTP 服务器,自动报告正在发送到该服务器。我需要将文件从我的 FTP 服务器下载到我的公共磁盘:

    //Search for the files.
    $fileName = array_filter(Storage::disk('ftp')->files("/reports"), function ($file)
    {
        //This get filename from the FTP server (/reports folder), that includes the current date (YYYY-MM-DD) in the file name.
        return preg_match('/'.date("Y", time()).'\-'.date("m", time()).'\-'.date("d", time()).'(.*)\.[a-zA-Z]{3}/m', $file);

    });

我可以使用上面的代码在我的 FTP 服务器上找到文件。有用。

接下来,我将文件从我的 FTP 服务器移动到我的公共服务器:

    //Set the new filename. We use this later on, when we move the file.
    $new_fileName = "milestone_report_".date("Y-m-d", time()).".xml";
    //$fileName[0] already includes the /reports path.
    $move_file = Storage::disk("public")->put("/reports/".$new_fileName."", Storage::disk('ftp')->get($fileName[0])); 

这也有效。最后我需要能够读取/获取 .xml 文件的内容,使用XmlParser. 我确实喜欢文档中的描述:

//Load the new file, so we can use it with the "xml parser package"
$xml = XmlParser::load("/storage/app/public/reports/".$new_fileName."");

但是,上面给了我以下错误:

无法从字符串解析 XML。

更新:

我试图从我的公共文件夹中加载一个 XML 文件www.domain.com/xmlfile.xml,这就像它应该的那样工作!

如何使用 XmlParser 从我的存储中加载?

标签: phpxmllaravel

解决方案


所以我想出了这个。XmlParser::load()我没有使用该函数,而是使用了该函数,并使用如下方式XmlParser::extract()提取了文件:Storage::disk()

$xml = XmlParser::extract(Storage::disk('public')->get("/reports/".$new_fileName.""));

它给我错误“无法从字符串解析 XML”的原因是因为以前我只获取到我的 XML 文件的路径(这只是一个字符串)。XmlParser需要将文件直接加载到方法中。


推荐阅读