A Simple Generator

Nested elements can be processed in place, or content processing can be packaged in functions:

for token in parser ():
   if token.isCharacters ():
      out.write (escapeText (token.characters))
   elif token.isStartElement ("verbatim"):
      out.write ("<TT>")
      processParaContent (parser, out)
      out.write ("</TT>")
   elif token.isStartElement ("graphic"):
      out.write ("<IMG SRC=\"" + unparsedEntity   \
         [token.attribute ["figname"]].systemId + \
         "\">")
New Models for Processing Text and XML with Python Sam Wilmott
Slide 34 of 51        www.wilmott.ca