Wrapper XMLReader class, for simple SAX-reading huge xml:
https://github.com/dkrnl/SimpleXMLReader
Usage example: http://github.com/dkrnl/SimpleXMLReader/blob/master/examples/example1.php
<?php
class SimpleXMLReader extends XMLReader
{
protected $callback = array();
public function registerCallback($name, $callback, $nodeType = XMLREADER::ELEMENT)
{
if (isset($this->callback[$nodeType][$name])) {
throw new Exception("Already exists callback $name($nodeType).");
}
if (!is_callable($callback)) {
throw new Exception("Already exists parser callback $name($nodeType).");
}
$this->callback[$nodeType][$name] = $callback;
return $this;
}
public function unRegisterCallback($name, $nodeType = XMLREADER::ELEMENT)
{
if (!isset($this->callback[$nodeType][$name])) {
throw new Exception("Unknow parser callback $name($nodeType).");
}
unset($this->callback[$nodeType][$name]);
return $this;
}
public function parse()
{
if (empty($this->callback)) {
throw new Exception("Empty parser callback.");
}
$continue = true;
while ($continue && $this->read()) {
if (isset($this->callback[$this->nodeType][$this->name])) {
$continue = call_user_func($this->callback[$this->nodeType][$this->name], $this);
}
}
}
public function expandXpath($path, $version = "1.0", $encoding = "UTF-8")
{
return $this->expandSimpleXml($version, $encoding)->xpath($path);
}
public function expandString($version = "1.0", $encoding = "UTF-8")
{
return $this->expandSimpleXml($version, $encoding)->asXML();
}
public function expandSimpleXml($version = "1.0", $encoding = "UTF-8", $className = null)
{
$element = $this->expand();
$document = new DomDocument($version, $encoding);
$node = $document->importNode($element, true);
$document->appendChild($node);
return simplexml_import_dom($node, $className);
}
public function expandDomDocument($version = "1.0", $encoding = "UTF-8")
{
$element = $this->expand();
$document = new DomDocument($version, $encoding);
$node = $document->importNode($element, true);
$document->appendChild($node);
return $document;
}
}
?>