Skip to content

Instantly share code, notes, and snippets.

@sgsinclair
Created June 17, 2013 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sgsinclair/5798130 to your computer and use it in GitHub Desktop.
Save sgsinclair/5798130 to your computer and use it in GitHub Desktop.
<?php
/* 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);
array_pop($stack);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment