2 A simple demo that reads in an XML document and displays the number of
3 elements and attributes as well as a tally of elements and attributes by name.
7 from collections
import defaultdict
9 from xml
.sax
import make_parser
, handler
11 class FancyCounter(handler
.ContentHandler
):
16 self
._elem
_types
= defaultdict(int)
17 self
._attr
_types
= defaultdict(int)
19 def startElement(self
, name
, attrs
):
21 self
._attrs
+= len(attrs
)
22 self
._elem
_types
[name
] += 1
24 for name
in attrs
.keys():
25 self
._attr
_types
[name
] += 1
27 def endDocument(self
):
28 print "There were", self
._elems
, "elements."
29 print "There were", self
._attrs
, "attributes."
31 print "---ELEMENT TYPES"
32 for pair
in self
._elem
_types
.items():
33 print "%20s %d" % pair
35 print "---ATTRIBUTE TYPES"
36 for pair
in self
._attr
_types
.items():
37 print "%20s %d" % pair
39 if __name__
== '__main__':
40 parser
= make_parser()
41 parser
.setContentHandler(FancyCounter())
42 parser
.parse(sys
.argv
[1])