Skip to content

Instantly share code, notes, and snippets.

@eduardelatorre
Last active May 31, 2016 11:45
Show Gist options
  • Save eduardelatorre/9a17d268f80cee34be642d10f348f78c to your computer and use it in GitHub Desktop.
Save eduardelatorre/9a17d268f80cee34be642d10f348f78c to your computer and use it in GitHub Desktop.
Read big XML files in PHP
<?php
/************************************************************************************
/* READ HUGE XML FILES
/* DO WITH DATA WHAT EVER YOU WANT, THIS CODE ONLY GET VALUES, NOT SHOWING ANYTHING
/* YOU MUST INCLUDE YOUR OWN CODE FOR SHOWING OR DOING WHAT EVER YOU WANT WITH DATA
/************************************************************************************/
// DISPLAY ERRORS
error_reporting(E_ALL);
ini_set('display_errors', '1');
// READ BIG COMPRESSED XML FILE
// COMPRESSED
$xmlfile = 'compress.zlib://example.xml.gz';
// OR WITHOUT COMPRESS
// $xmlfile = 'example.xml';
$reader = new XMLReader();
$reader->open($xmlfile);
$arrayattributtes = array();
while($reader->read()){
if($reader->nodeType == XMLReader::ELEMENT){
$xml = new SimpleXMLElement($reader->readOuterXML());
// TAG NAME
$tagname = $xml->getName();
// VALUE
if($xml){
$value = $xml;
}
else{
$value = '';
}
// ATTRIBUTES
if($xml->attributes()){
foreach($xml->attributes() as $attributename => $attributevalue) {
$arrayattributtes[$attributename] = $attributevalue;
}
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment