blob: 43161999a1120d0f702130e6858d0a76ab95f6fb [file] [log] [blame]
Fred Drakeac5f7482000-10-16 15:27:05 +00001import sys
2
3from xml.sax import make_parser, handler
4
5class FancyCounter(handler.ContentHandler):
6
7 def __init__(self):
8 self._elems = 0
9 self._attrs = 0
10 self._elem_types = {}
11 self._attr_types = {}
12
13 def startElement(self, name, attrs):
14 self._elems = self._elems + 1
15 self._attrs = self._attrs + len(attrs)
16 self._elem_types[name] = self._elem_types.get(name, 0) + 1
17
18 for name in attrs.keys():
19 self._attr_types[name] = self._attr_types.get(name, 0) + 1
20
21 def endDocument(self):
22 print "There were", self._elems, "elements."
23 print "There were", self._attrs, "attributes."
24
25 print "---ELEMENT TYPES"
26 for pair in self._elem_types.items():
27 print "%20s %d" % pair
28
29 print "---ATTRIBUTE TYPES"
30 for pair in self._attr_types.items():
31 print "%20s %d" % pair
32
33
34parser = make_parser()
35parser.setContentHandler(FancyCounter())
36parser.parse(sys.argv[1])