source: python/trunk/Demo/xml/elem_count.py

Last change on this file was 391, checked in by dmik, 11 years ago

python: Merge vendor 2.7.6 to trunk.

  • Property svn:eol-style set to native
File size: 1.1 KB
Line 
1"""
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
6import sys
7from collections import defaultdict
8
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
16 self._elem_types = defaultdict(int)
17 self._attr_types = defaultdict(int)
18
19 def startElement(self, name, attrs):
20 self._elems += 1
21 self._attrs += len(attrs)
22 self._elem_types[name] += 1
23
24 for name in attrs.keys():
25 self._attr_types[name] += 1
26
27 def endDocument(self):
28 print "There were", self._elems, "elements."
29 print "There were", self._attrs, "attributes."
30
31 print "---ELEMENT TYPES"
32 for pair in self._elem_types.items():
33 print "%20s %d" % pair
34
35 print "---ATTRIBUTE TYPES"
36 for pair in self._attr_types.items():
37 print "%20s %d" % pair
38
39if __name__ == '__main__':
40 parser = make_parser()
41 parser.setContentHandler(FancyCounter())
42 parser.parse(sys.argv[1])
Note: See TracBrowser for help on using the repository browser.