PHPackages                             benmorel/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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. benmorel/xml-streamer

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

benmorel/xml-streamer
=====================

Stream large XML files as DOM nodes with low memory consumption

0.6.0(6mo ago)1649.5k↓28.7%MITPHPPHP ^8.1CI passing

Since Aug 10Pushed 6mo ago2 watchersCompare

[ Source](https://github.com/BenMorel/XMLStreamer)[ Packagist](https://packagist.org/packages/benmorel/xml-streamer)[ GitHub Sponsors](https://github.com/BenMorel)[ RSS](/packages/benmorel-xml-streamer/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (7)Dependencies (3)Versions (8)Used By (0)

XML Streamer
============

[](#xml-streamer)

Stream large XML files as individual DOM elements with low memory consumption.

[![Build Status](https://github.com/BenMorel/XMLStreamer/workflows/CI/badge.svg)](https://github.com/BenMorel/XMLStreamer/actions)[![Coverage Status](https://camo.githubusercontent.com/bbb0b7167b1ee3851e2f3c690b37b91651e9458e5cde6fa8cae62704c459dddc/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f42656e4d6f72656c2f584d4c53747265616d65722f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/BenMorel/XMLStreamer?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/1a735f296e101eaeff663f1197cce2dd8f860ba45eb6443a000e23c93b34ead0/68747470733a2f2f706f7365722e707567782e6f72672f62656e6d6f72656c2f786d6c2d73747265616d65722f762f737461626c65)](https://packagist.org/packages/benmorel/xml-streamer)[![Total Downloads](https://camo.githubusercontent.com/083d6172359f7c66b7a9114c18730516d320d3fe720bb08a6a27bd001a8359bc/68747470733a2f2f706f7365722e707567782e6f72672f62656e6d6f72656c2f786d6c2d73747265616d65722f646f776e6c6f616473)](https://packagist.org/packages/benmorel/xml-streamer)[![License](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](http://opensource.org/licenses/MIT)

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

[](#installation)

This library is installable via [Composer](https://getcomposer.org/):

```
composer require benmorel/xml-streamer
```

Requirements
------------

[](#requirements)

This library requires:

- PHP 8.1 or later
- the [DOM](http://php.net/manual/en/book.dom.php) extension
- the [XMLReader](http://php.net/manual/en/book.xmlreader.php) extension

These extensions are enabled by default, and should be available in most PHP environments.

Project status &amp; release process
------------------------------------

[](#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](https://github.com/BenMorel/XMLStreamer/releases)for a list of changes introduced by each further `0.x.0` version.

Quickstart
----------

[](#quickstart)

Let's say you have a product feed containing a list of one million products, in the following format:

```

            1
            foo
            ...

        ...

            1000000
            bar
            ...

```

To read it product by product, you instantiate an `XMLStreamer` with the path to a `` 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](http://php.net/manual/en/class.domelement.php) object for each ``:

```
foreach ($streamer->stream('product-feed.xml') as $product) {
    /** @var DOMElement $product */
    echo $product->getElementsByTagName('name')->item(0)->textContent; // foo, ..., bar
}
```

### Querying with SimpleXML

[](#querying-with-simplexml)

If you prefer to work with SimpleXML, you can use [simplexml\_import\_dom()](http://php.net/manual/en/function.simplexml-import-dom.php). 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](http://php.net/manual/en/book.simplexml.php) extension, which is enabled by default.

### Return value

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

[](#configuration-options)

### Limiting the number of elements

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

[](#configuring-the-encoding)

The encoding of the source file is automatically read from the XML declaration:

```

```

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
--------------

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

###  Health Score

49

—

FairBetter than 95% of packages

Maintenance68

Regular maintenance activity

Popularity36

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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 ~441 days

Recently: every ~590 days

Total

7

Last Release

188d ago

PHP version history (4 changes)0.1.0PHP &gt;=7.1

0.4.0PHP ^7.2 || ^8.0

0.5.0PHP ^7.4 || ^8.0

0.6.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/57189121968030f0770811b461cc92f9c19c08f5c4767292f2ede48b7277cfad?d=identicon)[BenMorel](/maintainers/BenMorel)

---

Top Contributors

[![BenMorel](https://avatars.githubusercontent.com/u/1952838?v=4)](https://github.com/BenMorel "BenMorel (65 commits)")

---

Tags

xmldomXMLReader

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M229](/packages/masterminds-html5)[sabre/xml

sabre/xml is an XML library that you may not hate.

52832.2M131](/packages/sabre-xml)[fluentdom/fluentdom

A fluent api for the php dom extension.

337306.9k17](/packages/fluentdom-fluentdom)[veewee/xml

XML without worries

1835.9M29](/packages/veewee-xml)[dkrnl/simplexmlreader

Wrapper XMLReader class, for simple SAX-reading(and simple XPath-queries) of huge(testing over 1G file) xml.

112951.5k](/packages/dkrnl-simplexmlreader)[rct567/dom-query

DomQuery is a PHP library that allows easy 'jQuery like' DOM traversing and manipulation

134261.0k4](/packages/rct567-dom-query)

PHPackages © 2026

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