Skip to content

Instantly share code, notes, and snippets.

@DennisdeBest
Created January 27, 2017 09:00
Show Gist options
  • Save DennisdeBest/562a63fdcf227f023906e6d49ae12948 to your computer and use it in GitHub Desktop.
Save DennisdeBest/562a63fdcf227f023906e6d49ae12948 to your computer and use it in GitHub Desktop.
Iterate over elements using the XMLReader class
$xmlReader = new XMLReader();
$xmlReader->open(dirname(__FILE__) . self::XML_FILE_PATH);
while ($element = $this->getNextElementFromXML($xmlReader, "Rubrique")) {
}
/**
* return an array of the next element specified
* @param $xml
* @return bool
*/
private function getNextElementFromXML($xml, $element)
{
while ($xml->read()) {
switch ($xml->nodeType) {
case($xml::ELEMENT):
$name = $xml->name;
$value = $xml->value;
if ($xml->localName == $element) {
$outerXml = $xml->readOuterXML();
$xmlString = simplexml_load_string($outerXml);
$array = simpleXMLToArray($xmlString);
$xml->next();
return $array;
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment