# File: xmllib-example-2.py import xmllib import string, sys STYLESHEET = { # each element can contribute one or more style elements "quotation": {"style": "italic"}, "lang": {"weight": "bold"}, "name": {"weight": "medium"}, } class Parser(xmllib.XMLParser): # a simple styling engine def __init__(self, renderer): xmllib.XMLParser.__init__(self) self.__data = [] self.__tags = [] self.__renderer = renderer def load(self, file): while 1: s = file.read(8192) if not s: break self.feed(s) self.close() def handle_data(self, data): self.__data.append(data) def unknown_starttag(self, tag, attrs): if self.__data: text = string.join(self.__data, "") self.__renderer.text(self.__tags, text) self.__tags.append(tag) self.__data = [] def unknown_endtag(self, tag): self.__tags.pop() if self.__data: text = string.join(self.__data, "") self.__renderer.text(self.__tags, text) self.__data = [] class DumbRenderer: def __init__(self): self.cache = {} def text(self, tags, text): # render text in the style given by the tag stack tags = tuple(tags) style = self.cache.get(tags) if style is None: # figure out a combined style style = {} for tag in tags: s = STYLESHEET.get(tag) if s: style.update(s) self.cache[tags] = style # update cache # write to standard output sys.stdout.write("%s =>\n" % style) sys.stdout.write(" " + repr(text) + "\n") # # try it out r = DumbRenderer() c = Parser(r) c.load(open("samples/sample.xml")) ## $ python xmllib-example-2.py ## {'style': 'italic'} => ## 'I\'ve had a lot of developers come up to me and\012say, ## "I haven\'t had this much fun in a long time. It sure ## beats\012writing ' ## {'style': 'italic', 'weight': 'bold'} => ## 'Cobol' ## {'style': 'italic'} => ## '" -- ' ## {'style': 'italic', 'weight': 'medium'} => ## 'James Gosling' ## {'style': 'italic'} => ## ', on\012' ## {'weight': 'bold'} => ## 'Java' ## {'style': 'italic'} => ## '.'