PHP:如何循环遍历目录中的每个 XML 文件?

发布于 2024-08-25 17:43:12 字数 1440 浏览 10 评论 0原文

我正在构建一个简单的应用程序。它是在线订单系统的用户界面。

基本上,系统将像这样工作:

  1. 其他公司将他们的采购订单上传到我们的 FTP 服务器。这些订单是简单的 XML 文件(包含客户数据、地址信息、订购的产品和数量等内容......)

  2. 我用 HTML5、jQuery 和 CSS 构建了一个简单的用户界面 - 全部由 PHP 提供支持。

  3. PHP 读取订单内容(使用 SimpleXML 的内置功能)并将其显示在网页上。

所以,它是一个网络应用程序,应该始终在办公室的浏览器中运行。 PHP应用程序将显示所有订单的内容。每隔十五分钟左右,应用程序就会检查新订单。

如何循环遍历目录中的所有 XML 文件?

现在,我的应用程序能够读取单个 XML 文件的内容,并以良好的方式在页面上显示它。

我当前的代码如下所示:

// pick a random order that I know exists in the Order directory:
$xml_file = file_get_contents("Order/6366246.xml",FILE_TEXT);
$xml = new SimpleXMLElement($xml_file);

// start echo basic order information, like order number:
echo $xml->OrderHead->ShopPO;
// more information about the order and the customer goes here…

echo "<ul>";

// loop through each order line, and echo all quantities and products:
foreach ($xml->OrderLines->OrderLine as $orderline) {
    "<li>".$orderline->Quantity." st.</li>\n".
    "<li>".$orderline->SKU."</li>\n";
}
echo "</ul>";

// more information about delivery options, address information etc. goes here…

所以,这就是我的代码。很简单。它只需要做一件事——在屏幕上打印出所有订单文件的内容——这样我和我的同事就可以看到订单、确认并交付。

就是这样。

但现在 - 正如您所看到的 - 我一次选择一个订单,位于订单目录中。但是我如何循环遍历整个订单目录,并读取并显示每个订单的内容(如上)?

我被困住了。我真的不知道如何获取目录中的所有(xml)文件,然后对这些文件执行某些操作(例如读取它们并回显数据,就像我想要的那样)。

I'm building a simple application. It's a user interface to an online order system.

Basically, the system is going to work like this:

  1. Other companies upload their purchase orders to our FTP server. These orders are simple XML files (containing things like customer data, address information, ordered products and the quantities…)

  2. I've built a simple user interface in HTML5, jQuery and CSS — all powered by PHP.

  3. PHP reads the content of an order (using the built-in features of SimpleXML) and displays it on the web page.

So, it's a web app, supposed to always be running in a browser at the office. The PHP app will display the content of all orders. Every fifteen minutes or so, the app will check for new orders.

How do I loop through all XML files in a directory?

Right now, my app is able to read the content of a single XML file, and display it in a nice way on the page.

My current code looks like this:

// pick a random order that I know exists in the Order directory:
$xml_file = file_get_contents("Order/6366246.xml",FILE_TEXT);
$xml = new SimpleXMLElement($xml_file);

// start echo basic order information, like order number:
echo $xml->OrderHead->ShopPO;
// more information about the order and the customer goes here…

echo "<ul>";

// loop through each order line, and echo all quantities and products:
foreach ($xml->OrderLines->OrderLine as $orderline) {
    "<li>".$orderline->Quantity." st.</li>\n".
    "<li>".$orderline->SKU."</li>\n";
}
echo "</ul>";

// more information about delivery options, address information etc. goes here…

So, that's my code. Pretty simple. It only needs to do one thing — print out the content of all order files on the screen — so me and my colleagues can see the order, confirm it and deliver it.

That's it.

But right now — as you can see — I'm selecting one single order at a time, located in the Order directory. But how do I loop through the entire Order directory, and read aand display the content of each order (like above)?

I'm stuck. I don't really know how you get all (xml) files in a directory and then do something with the files (like reading them and echo out the data, like I want to).

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

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

发布评论

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

评论(2

踏雪无痕 2024-09-01 17:43:13

尝试 glob 函数:

$files = glob("Order/*xml");

if (is_array($files)) {

     foreach($files as $filename) {
        $xml_file = file_get_contents($filename, FILE_TEXT);
        // and proceed with your code
     }

}

Try the glob function:

$files = glob("Order/*xml");

if (is_array($files)) {

     foreach($files as $filename) {
        $xml_file = file_get_contents($filename, FILE_TEXT);
        // and proceed with your code
     }

}
苍风燃霜 2024-09-01 17:43:13

任何想要在 laravel storagege/xml 中存储的人,他们都可以从那里获取整个 xml 文件,并使用 Laravel 中的以下函数将其更改为 json,但只需稍作修改即可在核心 PHP 中使用,

   public function parse () {
    $files = File::allFiles(storage_path('xml'));

    //dd($files);

    foreach($files as $filename) {
        //dd($filename);
        $xml_file = file_get_contents($filename, FILE_TEXT);      
        $xml = simplexml_load_string($xml_file, "SimpleXMLElement", LIBXML_NOCDATA);
        $json = json_encode($xml);
        $array[] = json_decode($json,TRUE);
        //dd($array);
    }
    dd($array);
}

Anyone who looking to store in laravel storagege/xml,They could get from there entire xml file and change it to the json using below function in Laravel but with small modification can used in core PHP,

   public function parse () {
    $files = File::allFiles(storage_path('xml'));

    //dd($files);

    foreach($files as $filename) {
        //dd($filename);
        $xml_file = file_get_contents($filename, FILE_TEXT);      
        $xml = simplexml_load_string($xml_file, "SimpleXMLElement", LIBXML_NOCDATA);
        $json = json_encode($xml);
        $array[] = json_decode($json,TRUE);
        //dd($array);
    }
    dd($array);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文