Fred Drake | ac5f748 | 2000-10-16 15:27:05 +0000 | [diff] [blame] | 1 | import sys |
| 2 | |
| 3 | from xml.sax import make_parser, handler |
| 4 | |
| 5 | class 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 | |
| 34 | parser = make_parser() |
| 35 | parser.setContentHandler(FancyCounter()) |
| 36 | parser.parse(sys.argv[1]) |