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
53
54
55
56
57
58
59
60
61
62
|
#!/usr/bin/perl
use POSIX;
use XML::LibXML;
use XML::LibXML::Common;
$parser = new XML::LibXML;
sub getnum {
use POSIX;
my $str = shift;
$str =~ s/^\s+//;
$str =~ s/\s+$//;
$! = 0;
my($num, $unparsed) = strtod($str);
if (($str eq '') && ($unparsed != 0) && $!) {
return undef;
} else {
return $num;
}
}
sub parse {
($xml, $walk) = @_;
$doc = $parser->parse_file ($xml);
if ($walk) {
my $sum = 0;
my @nodes = $doc->findnodes("//*[count(child::*)>3]/*[number(@*) or number(text())]");
foreach $node(@nodes) {
$child = $node->getFirstChild();
if (($child)&&($child->nodeType == TEXT_NODE)) {
my $res = getnum($child->getData());
if (defined $res) { $sum += $res; next;}
}
@attrs = $node->attributes();
foreach $attr(@attrs) {
my $res = getnum($attr->value());
if (defined $res) { $sum += $res; last;}
}
}
# print "Sum: $sum\n";
}
undef($doc);
}
sub filename {
($i) = @_;
if (@ARGV > 1) { return $ARGV[1]; }
else { return "../xml.tmp/$i.xml"; }
}
my $walk_tree = $ENV{'walk_tree'};
if (@ARGV > 0) {
$iterations = $ARGV[0];
} else {
$iterations = 0;
}
parse(filename(0), $walk_tree);
for ($i=1;$i<=$iterations;$i++) {
parse(filename($i), $walk_tree);
}
|