O'Reilly logo

Python & XML by Christopher A. Jones, Fred L. Drake

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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] ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required