nvashenko/xml-streamer 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nvashenko/xml-streamer

Composer 安装命令:

composer require nvashenko/xml-streamer

包简介

Stream XML data and cast types to a provided classmap.

README 文档

README

Latest Version on Packagist Build Status

Content

Installation

You can install this package via composer:

composer require tbpixel/xml-streamer

Purpose

I found myself in need of a way to work with large XML data efficiently. The built in XMLReader PHP provides is fast and efficient, but can be a pain to work with at times. I wanted a dependency-free way to stream XML data and work with it using my provided classmap.

This package attempts to alleviate some of the headache of working with XMLReader, while also providing a collection of PSR-7 compatible XML streams. It offers a convenient way to iterate large XML data sets for reduced memory usage.

The optional client is also provided to allow for casting XML Strings to classmap objects.

Examples

Say we had an XML file called users.xml with the following data:

<?xml version='1.0' encoding='UTF-8'?>
<users>
    <user>
        <id>1</id>
        <name>John Doe</name>
    </user>
    <user>
        <id>2</id>
        <name>Theodor</name>
    </user>
</users>

With this package, we can simply create a new Client, pass it a PSR-7 compatible stream, and work with our data using our types.

$stream = new \TBPixel\XMLStreamer\Streams\FileReaderStream('users.xml', 1);
$client = new \TBPixel\XMLStreamer\Client($stream);

foreach ($client->iterate() as $simpleXMLElement) {
    // Do something with the SimpleXMLElement
}

$client->close(); // Closes the client's provided stream

We can also loop the client directly, as it implements the IteratorAggregate interface.

// Same as above loop
foreach ($client as $simpleXMLElement) {
    // Do something with the SimpleXMLElement
}

Iteration Depth

ReaderStream, the underlying XMLReader wrapper for the stream implementations found in this package, contains a final constructor argument called $depth. This is an integer (default to 0) or string which represents a depth to start iterating records found in an XML document.

The string can be the name of an XML tag, allowing the stream to iterate until it finds the tag rather than having to know the depth in advance.

Given the same data as above, we could rewrite our code snippet as follows:

$stream = new \TBPixel\XMLStreamer\Streams\FileReaderStream('users.xml', 'users');
$client = new \TBPixel\XMLStreamer\Client($stream);

foreach ($client->iterate() as $simpleXMLElement) {
    // Do something with the SimpleXMLElement
}

$client->close(); // Closes the client's provided stream

The previous value of 1 was acceptable for iterating this result set, but if the data was wrapped an arbitrary number of levels deep then this tag-name approach becomes convenient.

Automatic Casting

If we had a user which implemented the required CreateFromSimpleXML interface, we could also cast the SimpleXMLElement as we iterate for easier access.

use TBPixel\XMLStreamer\CreateFromSimpleXML;

class User implements CreateFromSimpleXML
{
    /** @var int */
    public $id;

    /** @var string */
    public $name;

    public function __construct(int $id, string $name)
    {
        $this->id = $id;
        $this->name = $name;
    }

    /**
     * Create a new instance from a Simple XML Element.
     *
     * @return static
     */
    public static function fromSimpleXML(\SimpleXMLElement $element)
    {
        return new static(
            (int) $element->id,
            (string) $element->name
        );
    }
}

Now when we create our client, lets just pass the FQCN to the clients classmap and casting will be automated.

$stream = new \TBPixel\XMLStreamer\Streams\FileReaderStream('users.xml');
$client = new \TBPixel\XMLStreamer\Client($stream, [
    'user' => User::class,
]);

foreach ($client->iterate() as $user) {
    // Work with the User object.
}

$client->close();

The clients second argument is an array of key value pairs mapping the XML element names to the FQCN.

Extending

Both the client and the streams are built ontop of PSR-7's Psr\Http\Message\StreamInterface, meaning it should be possible for you to swap out the stream with your own implementation. If you plan to use XMLReader but want a different resource handler, the TBPixel\XMLStreamer\ReaderStream is an abstract implementation which expects to process XML streams via XMLReader and handles most functionality well.

Contributing

Please see CONTRIBUTING for details.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Support Me

Hi! I'm a developer living in Vancouver, BC and boy is the housing market tough. If you wanna support me, consider following me on Twitter @TBPixel, or consider buying me a coffee.

License

The MIT License (MIT). Please see License File for more information.

nvashenko/xml-streamer 适用场景与选型建议

nvashenko/xml-streamer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 04 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 nvashenko/xml-streamer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 nvashenko/xml-streamer 我们能提供哪些服务?
定制开发 / 二次开发

基于 nvashenko/xml-streamer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-04-05