PHPackages                             tbpixel/xml-streamer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. tbpixel/xml-streamer

ActiveLibrary

tbpixel/xml-streamer
====================

Stream XML data and cast types to a provided classmap.

1.5.3(7y ago)341622[3 issues](https://github.com/TBPixel/xml-streamer/issues)MITPHPPHP ^7.2

Since Feb 6Pushed 7y ago2 watchersCompare

[ Source](https://github.com/TBPixel/xml-streamer)[ Packagist](https://packagist.org/packages/tbpixel/xml-streamer)[ RSS](/packages/tbpixel-xml-streamer/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (10)Dependencies (6)Versions (14)Used By (0)

TBPixel/XMLStreamer
===================

[](#tbpixelxmlstreamer)

[![Latest Version on Packagist](https://camo.githubusercontent.com/459f8dab7a2d50e9f6c1be642f0c02bca3c5ab53425be3b22378c30a5d856151/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f5442506978656c2f786d6c2d73747265616d65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/tbpixel/type-adapter)[![Build Status](https://camo.githubusercontent.com/9d6a2a7ec1fb894e2102861c035c28ae2eb5a1da714d5323d98f0f5f59ce31c4/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f5442506978656c2f786d6c2d73747265616d65722f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/TBPixel/type-adapter)

#### Content

[](#content)

- [Installation](#installation)
- [Purpose](#purpose)
    - [Examples](#examples)
    - [Automatic Casting](#automatic-casting)
- [Extending](#extending)
- [Contributing](#contributing)
- [Changelog](#changelog)
- [Support Me](#support-me)
- [License](#license)

Installation
------------

[](#installation)

You can install this package via composer:

```
composer require tbpixel/xml-streamer
```

Purpose
-------

[](#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

[](#examples)

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

```

        1
        John Doe

        2
        Theodor

```

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

[](#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

[](#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
---------

[](#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
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

### Support Me

[](#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](https://twitter.com/TBPixel), or consider [buying me a coffee](https://ko-fi.com/tbpixel).

License
-------

[](#license)

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

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity63

Established project with proven stability

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~3 days

Total

12

Last Release

2613d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e95171b17d11979d1512cd550d27ce6216fe493b39cf8a5853bda127ed470eb0?d=identicon)[TBPixel](/maintainers/TBPixel)

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tbpixel-xml-streamer/health.svg)

```
[![Health](https://phpackages.com/badges/tbpixel-xml-streamer/health.svg)](https://phpackages.com/packages/tbpixel-xml-streamer)
```

###  Alternatives

[league/uri-interfaces

Common tools for parsing and resolving RFC3987/RFC3986 URI

538204.9M23](/packages/league-uri-interfaces)[neuron-core/neuron-ai

The PHP Agentic Framework.

1.8k245.3k20](/packages/neuron-core-neuron-ai)[simplesamlphp/saml2

SAML2 PHP library from SimpleSAMLphp

30317.2M40](/packages/simplesamlphp-saml2)[php-heroku-client/php-heroku-client

A PHP client for the Heroku Platform API

24404.8k4](/packages/php-heroku-client-php-heroku-client)[phpro/http-tools

HTTP tools for developing more consistent HTTP implementations.

28137.8k](/packages/phpro-http-tools)[iwechatpay/openapi

为 wechatpay/wechatpay 增加IDE提示的接口描述包。

406.7k](/packages/iwechatpay-openapi)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
