Last active
May 12, 2019 17:47
-
-
Save DanLaufer/20515a721f7353a3bcc6fb2b0814958b to your computer and use it in GitHub Desktop.
PHP - XML - Get Node Count of a Glob of XML Docs
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
<?php | |
$xml_files = glob("./POC_LO_SET/*.xml"); | |
print "starting" . "\n"; | |
$overall_count = array(); | |
//$amount_to_try = 2; | |
foreach ($xml_files as $file) { | |
$doc = new DOMDocument(); | |
$doc->loadXML(file_get_contents($file)); | |
$xpath = new DOMXpath($doc); | |
$nodes = $xpath->query('//*'); | |
$names = array(); | |
foreach ($nodes as $node) { | |
$names[] = $node->nodeName; | |
} | |
$file_counts = array_count_values($names); | |
// Merge new array with overall array | |
foreach (array_keys($file_counts + $overall_count) as $key) { | |
$overall_count[$key] = (isset($file_counts[$key]) ? $file_counts[$key] : 0) + (isset($overall_count[$key]) ? $overall_count[$key] : 0); | |
} | |
// if we're testing on a subset, let's print the count of each | |
if (isset($amount_to_try) { | |
print_r($file_counts); | |
} | |
// break out of loop if number to try is set and gets to 0 | |
if (isset($amount_to_try) && !--$amount_to_try) break; | |
} | |
print_r($overall_count); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment