author robin
Fri, 23 Dec 2016 12:55:22 +0000
changeset 56 51219ad2b0bd
parent 11 a0a1adbc7b13
permissions -rw-r--r--
speed up windows 3.6 build

# uses pyexpat to build the tree. Yuk, globals,
# but want a quick speed comparison with pyRXP

import xml.parsers.expat

class ExpatTreeParser:
    """Crude and incomplete tree-builder based on expat.

    Need to add a few more handlers before it accurately
    deals with all relevant elements; but close enough
    for benchmark comparisons.  It (like expat) returns
    Unicode strings; we don't want to penalize it for
    this so leave them as Unicode."""
    def __init__(self):
        # fake top node makes it easy to initialize
        self.curNode = ('_FAKE_ROOT_',{},[],None)
        self.nodestack = [self.curNode]
    def handleStartElement(self, name, attrs):
        #print 'start element %s' % name
        newNode = (name, attrs, [], None)
        self.curNode = newNode

    def handleCharData(self, data):
        #print 'char data %s' % data

    def handleEndElement(self, name):
        #print 'end element %s' % name
        self.curNode = self.nodestack[-1]

    def parse(self, data):
        p = xml.parsers.expat.ParserCreate()
        p.StartElementHandler = self.handleStartElement
        p.EndElementHandler = self.handleEndElement
        p.CharacterDataHandler = self.handleCharData
        # will be the first child of our fake top node
        return self.curNode[2][0]

def expattree(data):
    return ExpatTreeParser().parse(data)