Created
April 9, 2014 18:39
-
-
Save mah0001/10301334 to your computer and use it in GitHub Desktop.
Get all XML paths using PHP/SimpleXML
This file contains hidden or 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
<?php | |
//usage | |
$paths_array=$print_paths("path-to-xml-file"); | |
var_dump($paths_array); | |
function print_paths($xml_file) | |
{ | |
$xml=simplexml_load_file($xml_file); | |
$output=array(); | |
$this->build_child_paths($xml,$xml->getName(),$output); | |
return $output; | |
} | |
function build_child_paths(&$xml_obj,$parent_path="/",&$output) | |
{ | |
foreach($xml_obj->children() as $child) | |
{ | |
$output[]=$parent_path.'/'.$child->getName(); | |
if ($child->children()) | |
{ | |
$this->build_child_paths($child,$parent_path.'/'.$child->getName(),$output); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment