2017 © Pedro Peláez
 

library xml-string-streamer

Stream large XML files with low memory consumption

image

prewk/xml-string-streamer

Stream large XML files with low memory consumption

  • Wednesday, October 4, 2017
  • by prewk
  • Repository
  • 20 Watchers
  • 204 Stars
  • 155,343 Installations
  • PHP
  • 7 Dependents
  • 0 Suggesters
  • 26 Forks
  • 8 Open issues
  • 31 Versions
  • 14 % Grown

The README.md

xml-string-streamer Build Status

Purpose

To stream XML files too big to fit into memory, with very low memory consumption. This library is a successor to XmlStreamer., (*1)

Installation

Legacy support

  • All versions below 1 support PHP 5.3 - 7.2
  • Version 1 and above support PHP 7.2+

With composer

Run composer require prewk/xml-string-streamer to install this package., (*2)

Usage

Let's say you have a 2 GB XML file gigantic.xml containing customer items that look like this:, (*3)

<?xml version="1.0" encoding="UTF-8"?>
<gigantic>
    <customer>
        <firstName>Jane</firstName>
        <lastName>Doe</lastName>
    </customer>
    ...
</gigantic>

Create a streamer and parse it:, (*4)

// Convenience method for creating a file streamer with the default parser
$streamer = Prewk\XmlStringStreamer::createStringWalkerParser("gigantic.xml");

while ($node = $streamer->getNode()) {
    // $node will be a string like this: "<customer><firstName>Jane</firstName><lastName>Doe</lastName></customer>"
    $simpleXmlNode = simplexml_load_string($node);
    echo (string)$simpleXmlNode->firstName;
}

Without the convenience method (functionally equivalient):, (*5)

use Prewk\XmlStringStreamer;
use Prewk\XmlStringStreamer\Stream;
use Prewk\XmlStringStreamer\Parser;

// Prepare our stream to be read with a 1kb buffer
$stream = new Stream\File("gigantic.xml", 1024);

// Construct the default parser (StringWalker)
$parser = new Parser\StringWalker();

// Create the streamer
$streamer = new XmlStringStreamer($parser, $stream);

// Iterate through the `<customer>` nodes
while ($node = $streamer->getNode()) {
    // $node will be a string like this: "<customer><firstName>Jane</firstName><lastName>Doe</lastName></customer>"
    $simpleXmlNode = simplexml_load_string($node);
    echo (string)$simpleXmlNode->firstName;
}

Convenience method for the UniqueNode parser:, (*6)

$streamer = Prewk\XmlStringStreamer::createUniqueNodeParser("file.xml", array("uniqueNode" => "customer"));

Parsers

Parser\StringWalker

Works like an XmlReader, and walks the XML tree node by node. Captures by node depth setting., (*7)

Parser\UniqueNode

A much faster parser that captures everything between a provided element's opening and closing tags. Special prerequisites apply., (*8)

Stream providers

Stream\File

Use this provider to parse large XML files on disk. Pick a chunk size, for example: 1024 bytes., (*9)

$CHUNK_SIZE = 1024;
$provider = new Prewk\XmlStringStreamer\Stream\File("large-xml-file.xml", $CHUNK_SIZE);

Stream\Stdin

Use this provider if you want to create a CLI application that streams large XML files through STDIN., (*10)

$CHUNK_SIZE = 1024;
$fsp = new Prewk\XmlStringStreamer\Stream\Stdin($CHUNK_SIZE);

Stream\Guzzle

Use this provider if you want to stream over HTTP with Guzzle. Resides in its own repo due to its higher PHP version requirements (5.5): https://github.com/prewk/xml-string-streamer-guzzle, (*11)

StringWalker Options

Usage

use Prewk\XmlStringStreamer;
use Prewk\XmlStringStreamer\Parser;
use Prewk\XmlStringStreamer\Stream;

$options = array(
    "captureDepth" => 3
);

$parser = new Parser\StringWalker($options);

Available options for the StringWalker parser

Option Default Description
(int) captureDepth 2 Depth we start collecting nodes at
(array) tags See example Supported tags
(bool) expectGT false Whether to support > in XML comments/CDATA or not
(array) tagsWithAllowedGT See example If expectGT is true, this option lists the tags with allowed > characters in them

Examples

captureDepth

Default behavior with a capture depth of 2:, (*12)

<?xml encoding="utf-8"?>
<root-node>
    <capture-me>
        ...
    </capture-me>
    <capture-me>
        ...
    </capture-me>
</root-node>

..will capture the <capture-me> nodes., (*13)

But say your XML looks like this:, (*14)

<?xml encoding="utf-8"?>
<root-node>
    <a-sub-node>
        <capture-me-instead>
            ...
        </capture-me-instead>
        <capture-me-instead>
            ...
        </capture-me-instead>
    </a-sub-node>
</root-node>

Then you'll need to set the capture depth to 3 to capture the <capture-me-instead> nodes., (*15)

Node depth visualized:, (*16)

<?xml?> <!-- Depth 0 because it's at the root and doesn't affect depth -->
<root-node> <!-- Depth 1 because it's at the root and increases depth -->
    <a-sub-node> <!-- Depth 2 because it has one ancestor and increases depth -->
        <capture-me-instead> <!-- Depth 3 because it has two ancestors and increases depth -->
        </capture-me-instead>
    </a-sub-node>
</root-node>

tags

Default value:, (*17)

array(
    array("<?", "?>", 0),
    array("<!--", "-->", 0),
    array("<![CDATA[", "]]>", 0),
    array("<!", ">", 0),
    array("</", ">", -1),
    array("<", "/>", 0),
    array("<", ">", 1)
),

First parameter: opening tag, second parameter: closing tag, third parameter: depth., (*18)

If you know that your XML doesn't have any XML comments, CDATA or self-closing tags, you can tune your performance by setting the tags option and omitting them:, (*19)

array(
    array("<?", "?>", 0),
    array("<!", ">", 0),
    array("</", ">", -1),
    array("<", ">", 1)
),

expectGT & tagsWithAllowedGT

You can allow the > character within XML comments and CDATA sections if you want. This is pretty uncommon, and therefore turned off by default for performance reasons., (*20)

Default value for tagsWithAllowedGT:, (*21)

array(
    array("<!--", "-->"),
    array("<![CDATA[", "]]>")
),

UniqueNode Options

Usage

use Prewk\XmlStringStreamer;
use Prewk\XmlStringStreamer\Parser;
use Prewk\XmlStringStreamer\Stream;

$options = array(
    "uniqueNode" => "TheNodeToCapture"
);

$parser = new Parser\UniqueNode($options);

Available options for the UniqueNode parser

Option Description
(string) uniqueNode Required option: Specify the node name to capture
(bool) checkShortClosing Whether to check short closing tag or not

Examples

uniqueNode

Say you have an XML file like this:, (*22)

<?xml encoding="utf-8"?>
<root-node>
    <stuff foo="bar">
        ...
    </stuff>
    <stuff foo="baz">
        ...
    </stuff>
    <stuff foo="123">
        ...
    </stuff>
</root-node>

You want to capture the stuff nodes, therefore set uniqueNode to "stuff"., (*23)

If you have an XML file with short closing tags like this:, (*24)

<?xml encoding="utf-8"?>
<root-node>
    <stuff foo="bar" />
    <stuff foo="baz">
        ...
    </stuff>
    <stuff foo="123" />
</root-node>

You want to capture the stuff nodes, therefore set uniqueNode to "stuff" and checkShortClosing to true., (*25)

But if your XML file look like this:, (*26)

<?xml encoding="utf-8"?>
<root-node>
    <stuff foo="bar">
        <heading>Lorem ipsum</heading>
        <content>
            <stuff>Oops, another stuff node</stuff>
        </content>
    </stuff>
    ...
</root-node>

..you won't be able to use the UniqueNode parser, because <stuff> exists inside of another <stuff> node., (*27)

Advanced Usage

Progress bar

You can track progress using a closure as the third argument when constructing the stream class. Example with the File stream using the StringWalker parser:, (*28)

use Prewk\XmlStringStreamer;
use Prewk\XmlStringStreamer\Stream\File;
use Prewk\XmlStringStreamer\Parser\StringWalker;

$file = "path/to/file.xml";

// Save the total file size
$totalSize = filesize($file);

// Construct the file stream
$stream = new File($file, 16384, function($chunk, $readBytes) use ($totalSize) {
    // This closure will be called every time the streamer requests a new chunk of data from the XML file
    echo "Progress: $readBytes / $totalSize\n";
});
// Construct the parser
$parser = new StringWalker;

// Construct the streamer
$streamer = new XmlStringStreamer($parser, $stream);

// Start parsing
while ($node = $streamer->getNode()) {
    // ....
}

You could of course do something more intelligent than spamming with echo., (*29)

Accessing the root element (version 0.7.0+)

Setting the parser option extractContainer tells the parser to gather everything before and after your intended child element capture. The results are available via the parser's getExtractedContainer() method., (*30)

Note: getExtractedContainer() will return different things depending on if you've streamed the whole file or not. If you need the containing XML data prematurely you can get it inside of the while loop, but it will just be the opening elements and therefore considered invalid XML by parsers such as SimpleXML., (*31)

use Prewk\XmlStringStreamer;
use Prewk\XmlStringStreamer\Stream\File;
use Prewk\XmlStringStreamer\Parser\StringWalker;

$file = "path/to/file.xml";

// Construct the file stream
$stream = new File($file, 16384);
// Construct the parser
$parser = new StringWalker(array(
    "extractContainer" => true, // Required option
));

// Construct the streamer
$streamer = new XmlStringStreamer($parser, $stream);

// Start parsing
while ($node = $streamer->getNode()) {
    // ....
}

// Get the containing XML
$containingXml = $parser->getExtractedContainer();

$xmlObj = simplexml_load_string($containingXml);
$rootElementName = $xmlObj->getName();
$rootElementFooAttribute = $xmlObj->attributes()->foo;

This method should be considered experimental, and may extract weird stuff in edge cases, (*32)

The Versions

04/10 2017

dev-master

9999999-dev https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

by Avatar prewk

03/10 2017

0.11.0

0.11.0.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

by Avatar prewk

29/07 2017

0.10.0

0.10.0.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3

 

The Development Requires

by Avatar prewk

23/03 2017

0.9.0

0.9.0.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar prewk

11/10 2016

0.8.0

0.8.0.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar prewk

27/09 2016

0.7.3

0.7.3.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar prewk

04/11 2015

0.7.2

0.7.2.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar prewk

08/05 2015

0.7.1

0.7.1.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar prewk

16/04 2015

0.7.0

0.7.0.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar prewk

26/09 2014

0.6.4

0.6.4.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar prewk

04/08 2014

0.6.3

0.6.3.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar prewk

31/07 2014

0.6.2

0.6.2.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar prewk

15/06 2014

0.6.1

0.6.1.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar prewk

15/06 2014

0.6.0

0.6.0.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar prewk

15/06 2014

dev-xml-reader-parser

dev-xml-reader-parser https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar prewk

15/06 2014

0.5.5

0.5.5.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar prewk

15/06 2014

0.5.4

0.5.4.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar prewk

14/06 2014

0.5.3

0.5.3.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar prewk

10/06 2014

0.5.2

0.5.2.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar prewk

03/06 2014

0.5.1

0.5.1.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar prewk

03/06 2014

0.5.0

0.5.0.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar prewk

01/06 2014

0.4.0

0.4.0.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar prewk

26/05 2014

0.3.2

0.3.2.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar prewk

24/05 2014

0.3.1

0.3.1.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar prewk

24/05 2014

0.3.0

0.3.0.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar prewk

19/05 2014

0.2.1

0.2.1.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar prewk

01/05 2014

0.2.0

0.2.0.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar prewk

01/05 2014

0.1.2

0.1.2.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar prewk

01/05 2014

0.1.1

0.1.1.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar prewk

28/04 2014

0.1.0

0.1.0.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar prewk

27/04 2014

0.0.4

0.0.4.0 https://github.com/prewk/xml-string-streamer

Stream large XML files with low memory consumption

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

by Avatar prewk