Converting XML to HTML
The PyXML package contains XML parsers, including PyExpat, as well as support for SAX and DOM, and much more. While learning the ropes of the PyXML package, it would be nice to have a comprehensive list of all the classes and methods. Since this is a programming book, it seems appropriate to write a Python program to extract the information we need—and in XML, no less!
Let’s generate an XML file that details each of the files in the PyXML package, the classes
therein, and the methods of the class. This process allows us to
generate quick, usable XML. Rather than a replacement for all the snazzy
code-to-documentation generators out there, Example 3-8 shows a simple, quick
way to generate XML that we can experiment with and use throughout the
examples in this chapter. After all, when manipulating XML, it helps to
have a few hundred thousand bytes of it sitting around to play with.
(This program also demonstrates the simplicity of examining all the files in a directory tree
in using the os.path.walk
function.)
Example 3-8. genxml.py
""" genxml.py Descends PyXML tree, indexing source files and creating XML tags for use in navigating the source. """ import os import sys from xml.sax.saxutils import escape def process(filename, fp): print "* Processing:", filename, # parse the file pyFile = open(filename) fp.write("<file name=\"" + filename + "\">\n") inClass = 0 line = pyFile.readline( ) while line: line = line.strip( ) if line.startswith("class") and line[-1] ...
Get Python & XML now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.