[prev in list] [next in list] [prev in thread] [next in thread] 

List:       python-xml-sig
Subject:    [XML-SIG] XmlWriter update
From:       gwachob () aimnet ! com (Gabe Wachob)
Date:       1999-01-20 19:49:41
Message-ID: Pine.GSO.4.05.9901201145250.26248-100000 () shell1 ! ncal ! verio ! com
[Download RAW message or body]

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


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic