borsodigerii/php-xml-chunker
Composer 安装命令:
composer require borsodigerii/php-xml-chunker
包简介
A lightweight, fast, and optimized XML file splitter with build in tag data validation, written with the XMLParser library. The main goal of this is to split an XML file into multiple small chunks (hence the name), then save it into multiple different little XML files.
关键字:
README 文档
README
The main goal of this library is to create chunks with predefined sizes from a big XML file (or to 'split' it into multiple chunks, so to say).
The algorithm was written using the XMLParser php library, which is capable of parsing an XML file line to line (or tag to tag) without state-control, and not by a string to string comparison or simple I/O operations. This attribute of the library makes it possible to implement validation on the said tags, everytime they are parsed.
With the correct charset specified, it can handle special characters, and also parse them for validation.
Installing
You can use this library by downloading the src\Chunker.php file, and using it directly, or by using composer as your package manager:
$ composer require borsodigerii/php-xml-chunker
Alternatively, you can add this library as a dependency in your composer.json file:
"require": { "borsodigerii/php-xml-chunker": "2.0.0" }
Then you just have to run composer update.
The minimum PHP version for this library to work, is >= 7.4.0
Usage
Simple Chunking
The implementation is Object-oriented, so in order to split the files, an instance of Chunker has to be created first.
An example of a simple Chunker instance without validation, with maximum 100 main tags/chunk, and with outputfile names of "out-{CHUNK}.xml":
$chunkSize = 100; $outputFilePrefix = "out-"; $xmlfile = "bigFile.xml"; $validationFunction = fn($data, $tag) => { return true; } $checkingTags = array(); $chunker = new Chunker\Chunker($xmlfile, $chunkSize, $outputFilePrefix, $validationFunction, $checkingTags);
Constructor variables
The following table contains the parameters that can be (and should be) passed to the constructor.
| Parameter | Type | Description | Default value | Is required |
|---|---|---|---|---|
| $xmlfile | string | The big XML file to be chunked | empty string | Yes |
| $chunkSize | int | The number of main tags maximum in a chunk | 100 | No |
| $outputFilePrefix | string | The prefix that will be used as the filename for the output chunks. Pattern: '{outputFilePrefix}{CHUNK-NUMBER}.xml' | 'out-' | No |
| $validationFunction | callable | The validator function that is used everytime a tag found, that is inside $checkingTags. If the tag data passes the validation, it will be included in the chunks, and will not be otherwise. It has to receive two parameters: first is the data that is inside the tag to be validated, and the second is the tag itself (both being strings). It has to return a boolean. | null | Yes |
| $checkingTags | array | An array of tags, where their data has to be validated using the $validationFunction callable. If we don't want any validation, we can pass an empty array to this parameter, or not specify it at all since it's not required. | empy array | No |
If any of the required parameters are empty/not specified, a Fatal error will be raised.
Launch the chunking!
After you created an instance of Chunker, and all the parameters were set, you can start the chunking process. You can do this with the Chunker::chunkXML method. An example is shown below:
// ... the instance is created in $chunker $chunker.chunkXML("item", "root");
This example will create xml chunks from the big file (if validation is enabled, then only the validated main tags will be included), with $chunkSize number of main tags (here it's called "item"). Every main tag is enclosed between one root tag (here it's called "root") in every file (so every chunked file will contain one root tag, and $chunkSize number of main tags inside it).
THe method returns the logging session's string conversion (see below for more information).
Logging
The class has an implemented logging feature. Everytime the Chunker::chunkXML is run, a new logging session is launched, which can be retrieved with the very same function. After its run, it returns the logging session converted into string:
// ... $log = $chunker.chunkXML(....); echo $log; /* [timestamp] Starting new chunking... [timestamp] .. [timestamp] .. */
It is really helpful, when something is not working for your needs, and has to be debugged from step to step. It is not neccessary to catch, so you can just call the function like its return value is void.
Examples
Basic validation
Lets say, that you have an XML file ("feed.xml") with a Shop root element, and multiple shopItem elements inside it (10.000+):
<?xml version="1.0" encoding="utf-8"?> <Shop> <...> <shopItem> <...> <weight_kg>5</weight_kg> <...> </shopItem> <shopItem> <...> <weight_kg>12</weight_kg> <...> </shopItem> <...> </Shop>
You want it to split into files named "feed-{chunk}.xml" containing 1000 shopItems maximum. And you also want to only include shopItems, that has a weight_kg tag inside, which can only be greater than 10 (or '10 kgs'). The solution is like the following:
$chunkSize = 1000; // Max. 1000 shopItems per chunk $xmlfile = "feed.xml"; // Input file $outPrefix = "feed-"; // Prefix for chunk-files $checkingTags = array("weight_kg"); // Tags to be validated (to which the validation function will be called) // validation function function validation($data, $tag) { if($tag == "weight_kg"){ if(!empty($data) && intval($data) > 10) return true; } return false; } // Tags to be counted with $chunkSize $mainTag = "shopItem"; // Root tag/element, that is only present once in the xml file $rootTag = "Shop"; // Creating the chunker instance, and running the Chunker $chunker = new Chunker\Chunker($xmlfile, $chunkSize, $outPrefix, "validation", $chekingTags); $chunker.chunkXML($mainTag, $rootTag);
borsodigerii/php-xml-chunker 适用场景与选型建议
borsodigerii/php-xml-chunker 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 188 次下载、GitHub Stars 达 5, 最近一次更新时间为 2023 年 10 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「xml」 「lightweight」 「charset」 「fast」 「split」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 borsodigerii/php-xml-chunker 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 borsodigerii/php-xml-chunker 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 borsodigerii/php-xml-chunker 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Load DOM document safety
Splot Framework. Lightweight, fully featured framework for rapid web development.
Added a method to Laravel response for handling xml response and also converting Eloquent return to XML.
Uses reflection to map XML to PHP objects.
Xml Template Engine
Alfabank REST API integration
统计信息
- 总下载量: 188
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-10-16