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
63
64
|
#! /usr/bin/python
import libxml2, sys, os
def parse(xml, walk_tree):
doc = libxml2.parseFile(xml)
if walk_tree:
ctxt = doc.xpathNewContext()
nodes = ctxt.xpathEval("//*[(number(@*) or number(text())) and (count(../child::*)>3)]")
# nodes = ctxt.xpathEval("//*[(@* or text()) and (count(../child::*)>3)]")
sum = 0.
for node in nodes:
child = node.children
if (child is not None and child.type == "text"):
try:
res = float(child.content)
if res:
sum += res
continue
except ValueError:
pass
#Exception is not actually thrown in my python, but just 0 is returned
#raise ValueError
attrs = node.get_properties()
for attr in attrs if attrs is not None else []:
try:
res = float(attr.children.content)
if res:
sum += res
break
except ValueError:
pass
#Exception is not actually thrown in my python, but just 0 is returned
#raise ValueError
ctxt.xpathFreeContext()
# print "Sum: %f" % sum
doc.freeDoc
try:
walk_tree = os.environ['walk_tree']
except:
walk_tree = None
if len(sys.argv) > 1:
iterations = int(sys.argv[1])
else:
iterations = 0
if len(sys.argv) > 2:
filename = lambda i: sys.argv[1]
else:
filename = lambda i: "../xml.tmp/" + str(i) + ".xml"
parse(filename(0), walk_tree)
for i in range(iterations):
parse(filename(i+1), walk_tree)
|