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
|
# A simple SAX application that counts the number of elements, attributes and
# processing instructions in a document.
from xml.sax import saxexts
from xml.sax import saxlib
import sys
class CounterHandler(saxlib.DocumentHandler):
def __init__(self):
self.elems=0
self.attrs=0
self.pis=0
def startElement(self,name,attrs):
self.elems=self.elems+1
self.attrs=self.attrs+len(attrs)
def processingInstruction(self,target,data):
self.pis=self.pis+1
# --- Main prog
if len(sys.argv)<2:
print "Usage: python saxstats.py <document>"
print
print " <document>: file name of the document to parse"
sys.exit(1)
# Load parser and driver
print "\nLoading parser..."
p=saxexts.make_parser()
ch=CounterHandler()
p.setDocumentHandler(ch)
# Ready, set, go!
print "Starting parse..."
OK=0
try:
p.parse(sys.argv[1])
OK=1
except IOError,e:
print "\nERROR: "+sys.argv[1]+": "+str(e)
except saxlib.SAXException,e:
print "\nERROR: "+str(e)
print "Parse complete:"
print " Elements: %d" % ch.elems
print " Attributes: %d" % ch.attrs
print " Proc instrs: %d" % ch.pis
|