Skip to content

Instantly share code, notes, and snippets.

@mah0001
Created April 9, 2014 18:39
Show Gist options
  • Save mah0001/10301334 to your computer and use it in GitHub Desktop.
Save mah0001/10301334 to your computer and use it in GitHub Desktop.
Get all XML paths using PHP/SimpleXML
<?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