[XML-SIG] XmlWriter update

Gabe Wachob gwachob@aimnet.com
Wed, 20 Jan 1999 11:49:41 -0800 (PST)


On Wed, 20 Jan 1999, Andrew M. Kuchling wrote:

> Christian Tismer writes about Jeff Johnson's XmlWriter patch:
> >Hi XMLers!
> >I found this one quite useful.
> >Will it make it into the lib?
> 
> 	I haven't gotten around to looking at the patch, but
> definitely would like to include it; Jeff's code submissions have been
> fine in the past, so it'll probably wind up applied to the CVS tree.
> (I've been thinking of issuing a 0.5.1 updated release, but haven't
> had time for XML hacking lately.)
> 
> >Furthermore, is anybody interested in a prettyprint mode,
> >(with some indentation), or has that been done already?
> 
> 	That would be very useful.

Quick-n-ugly (and I do mean ugly) prettyprint into HTML -- if someone
wants to make it better:

from xml.sax import saxlib
from xml.sax import saxexts
import sys

class XMLPrettyPrint(saxlib.HandlerBase):
    """
    Pretty print an XML source tree in HTML with colors, etc
    """
    def __init__(self):
	totalstring=""

    def startElement(self, name, attrs):
	string= "<ul><font color=\"#1155ff\">&lt;"+name
	if attrs.getLength() > 0:
	    for key in attrs.keys():
		string=string+ " <font color=\"#339922\">"+key+"=\"<font color=\"#226600\">"+attrs[key]+"</font>\"</font>"
	self.totalstring=self.totalstring+(string+"&gt;</font>")

    def characters(self, ch, start, length):
	self.totalstring=self.totalstring+"<ul>"+(ch[start:start+length])+"</ul>"

    def endElement(self, name):
	self.totalstring=self.totalstring+ "<font color=\"#1155ff\">&lt;/"+name+"&gt;</font></ul>"

    def startDocument(self):
	self.totalstring=self.totalstring+ "<tt>"

    def endDocument(self):
	self.totalstring=self.totalstring+ "</tt>"

    def processingInstruction(self, target, data):
	self.totalstring=self.totalstring+ "<font color=\"#ff2244\">&lt;?"+target+" "+data+"?&gt;</font>"


if __name__=="__main__":
    myparser=saxexts.make_parser()
    myxpp=XMLPrettyPrint()
    myparser.setDocumentHandler(myxpp)
    myparser.parseFile(sys.stdin)
    print myxpp.totalstring

------------------------------------------------------------------------
Gabe Wachob - http://www.findlaw.com - http://www.aimnet.com/~gwachob
As of today, the U.S. Constitution has been in force for 76,913 days
When this message was sent, there were 29,851,818 seconds before Y2K