Skip to content

Instantly share code, notes, and snippets.

@catalint
Last active August 29, 2015 14:01
Show Gist options
  • Save catalint/76abfc125631670649e2 to your computer and use it in GitHub Desktop.
Save catalint/76abfc125631670649e2 to your computer and use it in GitHub Desktop.
Simple class to parse BIG xml files without loading them in memory. It parses one tag a time.
$reader = new XMLReaderToSimpleXML();
$reader->open($filePath);
while($package = $reader->nextElement('Package')){
parsePackage($package); // edit this to do the parsing stuff
}
class XMLReaderToSimpleXML extends XMLReader{
function nextElement($tagName){
while($this->read()){
if($this->localName == $tagName && $this->nodeType == XMLReader::ELEMENT){
return simplexml_load_string($this->readOuterXml());
}
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment