2bbf21ec82f8d5274086c3da0df6130e.png

PHP 7 Stream Parser 是由 Sergio Ródenas 开发的扩展包,用于为 PHP 提供多格式文件流解析。使用 pull 解析器的流在大型文档中比 DOM 加载更加有效。

下面的示例展示了结合 Laravel 队列使用流式解析器:

StreamParser::xml("https://example.com/users.xml")->each(function(Collection $user){

dispatch(new App\Jobs\SendEmail($user));

});

以下方的 XML 数据为例,流式解析器与「集合」协作得很好,结果非常清晰:

The Iliad and The Odyssey

12.95

Best translation I've read.

I like other versions better.

[...]

PHP的代码以及输出的例子:

StreamParser::xml("https://example.com/books.xml")->each(function(Collection $book){

var_dump($book);

var_dump($book->get('comments')->toArray());

});

// 输出

class Illuminate\Support\Collection#19 (1) {

protected $items =>

array(4) {

'ISBN' =>

string(13) "10-000000-001"

'title' =>

string(25) "The Iliad and The Odyssey"

'price' =>

string(5) "12.95"

'comments' =>

class Illuminate\Support\Collection#17 (1) {

protected $items =>

array(2) {

...

}

}

}

}

array(2) {

[0] =>

array(2) {

'rating' =>

string(1) "4"

'userComment' =>

string(27) "Best translation I've read."

}

[1] =>

array(2) {

'rating' =>

string(1) "2"

'userComment' =>

string(29) "I like other versions better."

}

}

下面是使用 CSV 的使用示例:

title,price,comments

The Iliad and The Odyssey,12.95,"Best translation I've read.,I like other versions better."

Anthology of World Literature,24.95,"Needs more modern literature.,Excellent overview of world literature."

接下来是使用文件流解析器的代码和输出:

StreamParser::csv("https://example.com/books.csv")->each(function(Collection $book){

var_dump($book->get('comments')->last());

});

// 输出

string(29) "I like other versions better."

string(39) "Excellent overview of world literature."

了解更多

你可以在 GitHub 上了解关于此安装包的更多信息,并且可以使用 composer require Rodenastyle/stream-parser 来进行安装使用。README 还提到了这篇文章,这里有关于 processing XML files via DOM vs. Streaming 的说明。

本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接

我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐