/xmlbench/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/xmlbench/trunk

« back to all changes in this revision

Viewing changes to parse/scripts/libxml.py

  • Committer: Suren A. Chilingaryan
  • Date: 2009-10-02 01:16:02 UTC
  • Revision ID: csa@dside.dyndns.org-20091002011602-ut1jl0go12npun6y
DOM walking for all Libxml bindings: ruby, python, perl, php, lisp

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#! /usr/bin/python
2
2
 
3
 
import libxml2, sys
4
 
 
5
 
def parse(xml):
 
3
import libxml2, sys, os
 
4
 
 
5
 
 
6
def parse(xml, walk_tree):
6
7
    doc = libxml2.parseFile(xml)
 
8
    if walk_tree:
 
9
        ctxt = doc.xpathNewContext()
 
10
        nodes = ctxt.xpathEval("//*[(number(@*) or number(text())) and (count(../child::*)>3)]")
 
11
#       nodes = ctxt.xpathEval("//*[(@* or text()) and (count(../child::*)>3)]")
 
12
        sum = 0.
 
13
 
 
14
        for node in nodes:
 
15
            if (node.children is not None and node.children.type == "text"):
 
16
                try:
 
17
                    res = float(node.children.content) 
 
18
                    if res:
 
19
                        sum += res
 
20
                        continue
 
21
                except ValueError:
 
22
                    pass
 
23
                    #Exception is not actually thrown in my python, but just 0 is returned
 
24
                    #raise ValueError;
 
25
 
 
26
            attrs = node.get_properties()
 
27
            for attr in attrs if attrs is not None else []:
 
28
                try:
 
29
                    res = float(attr.children.content)
 
30
                    if res:
 
31
                        sum += res
 
32
                        break
 
33
                except ValueError:
 
34
                    pass
 
35
                    #Exception is not actually thrown in my python, but just 0 is returned
 
36
                    #raise ValueError;
 
37
 
 
38
                
 
39
        ctxt.xpathFreeContext()
 
40
#       print "Sum: %f" % sum
7
41
    doc.freeDoc
8
42
 
 
43
 
 
44
walk_tree = os.environ['walk_tree']
 
45
 
9
46
if len(sys.argv) > 1:
10
47
    iterations = int(sys.argv[1])
11
48
else:
17
54
else:
18
55
    filename = lambda i: "../xml.tmp/" + str(i) + ".xml"
19
56
 
20
 
parse(filename(0))
 
57
parse(filename(0), walk_tree)
21
58
 
22
59
for i in range(iterations):
23
 
    parse(filename(i+1))
 
60
    parse(filename(i+1), walk_tree)