1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
#!/usr/bin/php
<?
function parse($xml, $walk_tree) {
$doc = new DOMDocument();
$doc->load($xml);
if ($walk_tree) {
$sum = 0;
$xp = new DOMXPath($doc);
$nodes = $xp->query("//*[count(child::*)>3]/*[number(@*) or number(text())]");
foreach ($nodes as $node) {
$child = $node->firstChild;
if (($child)&&($child instanceof DOMText)) {
$value = &$child->textContent;
if (is_numeric($value)) {
$sum += $value;
continue;
}
}
if ($node->attributes) {
foreach ($node->attributes as $attr) {
if (is_numeric($attr->value)) {
$sum += $attr->value;
break;
}
}
}
}
# echo "Sum: {$sum}\n";
}
//echo $doc->saveXML();
unset($doc);
}
$argv = $_SERVER['argv'];
$walk_tree = $_SERVER['walk_tree'];
if (sizeof($argv) > 1)
$iterations = $argv[1];
else
$iterations = 0;
if (sizeof($argv) > 2)
$filename = create_function('$i', 'return "' . $argv[2] . '";');
else
$filename = create_function('$i', 'return "../xml.tmp/{$i}.xml";');
parse($filename(0), $walk_tree);
for ($i=1;$i<=$iterations;$i++) {
parse($filename($i), $walk_tree);
}
?>
|