3 from xml
.sax
import make_parser
, handler
5 class FancyCounter(handler
.ContentHandler
):
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])