benmorel/xml-streamer
Composer 安装命令:
composer require benmorel/xml-streamer
包简介
Stream large XML files as DOM nodes with low memory consumption
关键字:
README 文档
README
Stream large XML files as individual DOM elements with low memory consumption.
Installation
This library is installable via Composer:
composer require benmorel/xml-streamer
Requirements
This library requires:
These extensions are enabled by default, and should be available in most PHP environments.
Project status & release process
This library is under development.
The current releases are numbered 0.x.y. When a non-breaking change is introduced (adding new methods, optimizing
existing code, etc.), y is incremented.
When a breaking change is introduced, a new 0.x version cycle is always started.
It is therefore safe to lock your project to a given release cycle, such as 0.6.*.
If you need to upgrade to a newer release cycle, check the release history
for a list of changes introduced by each further 0.x.0 version.
Quickstart
Let's say you have a product feed containing a list of one million products, in the following format:
<?xml version="1.0" encoding="UTF-8"?> <feed> <products> <product> <id>1</id> <name>foo</name> ... </product> ... <product> <id>1000000</id> <name>bar</name> ... </product> </products> </feed>
To read it product by product, you instantiate an XMLStreamer with the path to a <product> element:
use BenMorel\XMLStreamer\XMLStreamer; $streamer = new XMLStreamer('feed', 'products', 'product');
Any element in the document that does not match this path will be ignored.
You can then proceed to streaming the file with a generator, that will yield a DOMElement object for each <product>:
foreach ($streamer->stream('product-feed.xml') as $product) { /** @var DOMElement $product */ echo $product->getElementsByTagName('name')->item(0)->textContent; // foo, ..., bar }
Querying with SimpleXML
If you prefer to work with SimpleXML, you can use simplexml_import_dom(). SimpleXML requires that you wrap your element in a DOMDocument before importing it:
foreach ($streamer->stream('product-feed.xml') as $product) { /** @var DOMElement $product */ $document = new \DOMDocument(); $document->appendChild($product); $element = simplexml_import_dom($product); echo $element->name; // foo, ..., bar }
This requires the SimpleXML extension, which is enabled by default.
Return value
After all elements have been processed, the generator returns the number of streamed elements:
$products = $streamer->stream('product-feed.xml'); foreach ($products as $product) { /* ... */ } $productCount = $products->getReturn();
Configuration options
Limiting the number of elements
If you need to get just a preview of the XML file, you can set the maximum number of elements to stream:
$streamer->setMaxElements(10);
With this configuration, XMLStreamer would yield at most 10 elements, and ignore further entries.
Configuring the encoding
The encoding of the source file is automatically read from the XML declaration:
<?xml version="1.0" encoding="UTF-8"?>
If your XML file is missing the encoding, you can specify it manually:
$streamer->setEncoding('ISO-8859-1');
Note that this only specifies the input file encoding. The DOMElement output is always UTF-8.
Error handling
If an error occurs at any point (error opening or reading the file, malformed document), an XMLReaderException is thrown.
Note that the streaming may have already been started when the exception is thrown, so the generator may have already yielded a number of elements.
benmorel/xml-streamer 适用场景与选型建议
benmorel/xml-streamer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 54.93k 次下载、GitHub Stars 达 16, 最近一次更新时间为 2018 年 08 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「xml」 「dom」 「XMLReader」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 benmorel/xml-streamer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 benmorel/xml-streamer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 benmorel/xml-streamer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A library that provides basic utilities for creating HTML documents.
A bridge between SimpleXML and the DOM extension, plus a bunch of convenience methods
BigPipe: Pipelining web pages for high performance built in PHP.
Extends XMLReader PHP class, for simple SAX-reading and XPath queries of huge XML files.
Load DOM document safety
Added a method to Laravel response for handling xml response and also converting Eloquent return to XML.
统计信息
- 总下载量: 54.93k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 16
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-08-08