Created
January 27, 2017 09:00
-
-
Save DennisdeBest/562a63fdcf227f023906e6d49ae12948 to your computer and use it in GitHub Desktop.
Iterate over elements using the XMLReader class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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