Skip to content

Instantly share code, notes, and snippets.

@DanLaufer
Last active May 12, 2019 17:47
Show Gist options
  • Save DanLaufer/20515a721f7353a3bcc6fb2b0814958b to your computer and use it in GitHub Desktop.
Save DanLaufer/20515a721f7353a3bcc6fb2b0814958b to your computer and use it in GitHub Desktop.
PHP - XML - Get Node Count of a Glob of XML Docs
<?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