blob: 99d6ca949899efe0b40ba2ac61b874c56c911090 [file] [log] [blame]
Georg Brandlbc470d52009-10-11 15:56:06 +00001"""
2A simple demo that reads in an XML document and displays the number of
3elements and attributes as well as a tally of elements and attributes by name.
4"""
5
Fred Drakeac5f7482000-10-16 15:27:05 +00006import sys
Georg Brandlbc470d52009-10-11 15:56:06 +00007from collections import defaultdict
Fred Drakeac5f7482000-10-16 15:27:05 +00008
9from xml.sax import make_parser, handler
10
11class FancyCounter(handler.ContentHandler):
12
13 def __init__(self):
14 self._elems = 0
15 self._attrs = 0
Georg Brandlbc470d52009-10-11 15:56:06 +000016 self._elem_types = defaultdict(int)
17 self._attr_types = defaultdict(int)
Fred Drakeac5f7482000-10-16 15:27:05 +000018
19 def startElement(self, name, attrs):
Georg Brandlbc470d52009-10-11 15:56:06 +000020 self._elems += 1
21 self._attrs += len(attrs)
22 self._elem_types[name] += 1
Fred Drakeac5f7482000-10-16 15:27:05 +000023
Skip Montanaro1e8ce582007-08-06 21:07:53 +000024 for name in attrs.keys():
Georg Brandlbc470d52009-10-11 15:56:06 +000025 self._attr_types[name] += 1
Fred Drakeac5f7482000-10-16 15:27:05 +000026
27 def endDocument(self):
Collin Winter6f2df4d2007-07-17 20:59:35 +000028 print("There were", self._elems, "elements.")
29 print("There were", self._attrs, "attributes.")
Fred Drakeac5f7482000-10-16 15:27:05 +000030
Collin Winter6f2df4d2007-07-17 20:59:35 +000031 print("---ELEMENT TYPES")
Skip Montanaro1e8ce582007-08-06 21:07:53 +000032 for pair in self._elem_types.items():
Collin Winter6f2df4d2007-07-17 20:59:35 +000033 print("%20s %d" % pair)
Fred Drakeac5f7482000-10-16 15:27:05 +000034
Collin Winter6f2df4d2007-07-17 20:59:35 +000035 print("---ATTRIBUTE TYPES")
Skip Montanaro1e8ce582007-08-06 21:07:53 +000036 for pair in self._attr_types.items():
Collin Winter6f2df4d2007-07-17 20:59:35 +000037 print("%20s %d" % pair)
Fred Drakeac5f7482000-10-16 15:27:05 +000038
Georg Brandlbc470d52009-10-11 15:56:06 +000039if __name__ == '__main__':
40 parser = make_parser()
41 parser.setContentHandler(FancyCounter())
42 parser.parse(sys.argv[1])