Add example
[pytest.git] / Demo / xml / elem_count.py
blob7b53189c7c436cfa9aeb12b53bb5a21dc6013659
1 import sys
3 from xml.sax import make_parser, handler
5 class FancyCounter(handler.ContentHandler):
7 def __init__(self):
8 self._elems = 0
9 self._attrs = 0
10 self._elem_types = {}
11 self._attr_types = {}
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
18 for name in attrs.keys():
19 self._attr_types[name] = self._attr_types.get(name, 0) + 1
21 def endDocument(self):
22 print "There were", self._elems, "elements."
23 print "There were", self._attrs, "attributes."
25 print "---ELEMENT TYPES"
26 for pair in self._elem_types.items():
27 print "%20s %d" % pair
29 print "---ATTRIBUTE TYPES"
30 for pair in self._attr_types.items():
31 print "%20s %d" % pair
34 parser = make_parser()
35 parser.setContentHandler(FancyCounter())
36 parser.parse(sys.argv[1])