Skip to content

Instantly share code, notes, and snippets.

@fprochazka
Created June 2, 2012 21:16
Show Gist options
  • Save fprochazka/2859976 to your computer and use it in GitHub Desktop.
Save fprochazka/2859976 to your computer and use it in GitHub Desktop.
PHP Xml Iterator
<?php
/**
* @author Filip Procházka <filip.prochazka@kdyby.org>
*/
class XmlReaderIterator extends Nette\Object implements \Iterator
{
/**
* @var \XMLReader
*/
private $xmlReader;
/**
* @var string
*/
private $xmlFile;
/**
* @var string
*/
private $itemName;
/**
* @var int
*/
private $counter = 0;
/**
* @var SimpleXmlElement
*/
private $current;
/**
* @var integer
*/
private $size;
/**
* @var integer
*/
private $read;
/**
* @param string $xmlFile
*/
public function __construct($xmlFile)
{
$this->xmlFile = $xmlFile;
$this->size = filesize($xmlFile);
}
/**
*/
public function rewind()
{
$this->__destruct();
$this->xmlReader = new XMLReader();
$this->xmlReader->open($this->xmlFile);
$this->next();
}
/**
* @return \SimpleXmlElement
*/
public function current()
{
return $this->current;
}
/**
* @return int
*/
public function key()
{
return $this->counter;
}
/**
* @return \SimpleXmlElement
*/
public function next()
{
if (!$this->itemName) {
$this->xmlReader->read(); // root element?
do {
$this->xmlReader->read(); // item
$this->itemName = $this->xmlReader->name;
} while ($this->itemName === '#text' && $this->itemName);
}
if ($this->current) {
$this->xmlReader->next($this->itemName);
}
if ($element = $this->xmlReader->readOuterXML()) {
$this->read += strlen($element);
$this->current = new SimpleXmlElement($element);
} else {
$this->current = NULL;
}
$this->counter++;
return $this->current;
}
/**
* @return bool
*/
public function valid()
{
return $this->current !== NULL;
}
/**
*/
public function __destruct()
{
$this->counter = $this->read = 0;
$this->current = NULL;
if ($this->xmlReader) {
$this->xmlReader->close();
}
}
/**
* @return int
*/
public function getProgress()
{
return ($this->read / $this->size) * 100;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment