2017 © Pedro Peláez
 

library xml-streamer

Stream large XML files with low memory consumption.

image

prewk/xml-streamer

Stream large XML files with low memory consumption.

  • Monday, March 9, 2015
  • by prewk
  • Repository
  • 7 Watchers
  • 49 Stars
  • 6,044 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 26 Forks
  • 0 Open issues
  • 4 Versions
  • 11 % Grown

The README.md

XmlStreamer Build Status

This project is discontinued, I recommend using its successor xml-string-streamer instead., (*1)

About

Written by oskar.thornblad@gmail.com., (*2)

Contributions from:, (*3)

  • Valiton GmbH
  • Michael Härtl

Licensed under the MIT license., (*4)

Installation

Install with composer by adding the following to your composer.json file:, (*5)

{
    "require": {
        "prewk/xml-streamer": "dev-master"
    }
}
````

Then, run `composer install` (assuming you have [composer](https://getcomposer.org/) installed.)

# Usage

Extend the class and implement the `processNode()` method.

## Example


```php
<?php
class SimpleXmlStreamer extends \Prewk\XmlStreamer
{
    public function processNode($xmlString, $elementName, $nodeIndex)
    {
        $xml = simplexml_load_string($xmlString);
        $something = (string)$xml->Something->SomethingElse->ReadThis;
        echo "$nodeIndex: Extracted string '$something' from parent node '$elementName'\n";     
        return true;
    }
}

$streamer = new SimpleXmlStreamer("myLargeXmlFile.xml");
if ($streamer->parse()) {
    echo "Finished successfully";
} else {
    echo "Couldn't find root node";
}

Advanced example

To improve performance on DB inserts you can also make use of the chunkCompleted() method. It gets called after a chunk of data was processed., (*6)

<?php
class SimpleXmlStreamer extends \Prewk\XmlStreamer
{
    protected $pdo;
    protected $sql = array();
    protected $values = array();

    /**
     * Called after the constructor completed class setup
     */
    public function init()
    {
        $this->pdo = new PDO('mysql:host=localhost;dbname=test', 'user','pass');
    }

    public function processNode($xmlString, $elementName, $nodeIndex)
    {
        $xml = simplexml_load_string($xmlString);
        $this->sql[] = '(?,?,?)';
        $this->values[] = (string)$xml->name;
        $this->values[] = (string)$xml->email;
        $this->values[] = (string)$xml->phone;
    }

    /**
     * Called after a file chunk was processed (16KB by default, see constructor)
     */
    public function chunkCompleted()
    {
        if($this->sql===array()) {
            return;
        }
        $command = $this->pdo->prepare('INSERT INTO mytable VALUES '.implode(',',$this->sql));
        $command->execute($this->values);

        $this->sql = $this->values = array();
    }
}

The Versions

09/03 2015

dev-master

9999999-dev https://github.com/prewk/XmlStreamer

Stream large XML files with low memory consumption.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Avatar prewk

09/03 2015

0.5.0

0.5.0.0 https://github.com/prewk/XmlStreamer

Stream large XML files with low memory consumption.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Avatar prewk

26/04 2014

0.4.2

0.4.2.0 https://github.com/prewk/XmlStreamer

Stream large XML files with low memory consumption.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Avatar prewk

26/04 2014

0.4.1

0.4.1.0 https://github.com/prewk/XmlStreamer

Stream large XML files with low memory consumption.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Avatar prewk