Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
/* this is a simple script to generate a tree hierarchy based on element tags
by Stéfan Sinclair, no license (use and abuse as you wish, no reponsibility here) */
ini_set("memory_limit", "512M");
$xml = simplexml_load_file(dirname(__FILE__) . '/tagsOnly.xml');
$paths = array();
getChildren($xml, array('ORLANDO'));
echo json_encode(outputChildren('ORLANDO/ENTRY'));
function outputChildren($path) {
global $paths, $path_keys;
$children = array();
if (isset($path_keys[$path])) {
foreach (array_keys($path_keys[$path]) as $key) {
if ($paths["$path/$key"]>50) {
$children[] = outputChildren("$path/$key");
$result = array(
"name" => substr($path, strrpos($path, '/')+1),
"size" => $paths[$path]
if ($children) {$result['children']=$children;}
return $result;
function getChildren($xml, $stack) {
global $paths, $path_keys;
foreach ($xml->children() as $element => $child) {
$path_keys[implode($stack, "/")][$element]++;
array_push($stack, $element);
$paths[implode($stack, "/")]++;
getChildren($child, $stack);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.