[XML-SIG] Whitespace handling in XMLWriter
Radestock, Guenter
guenter.radestock@sap.com
Sun, 25 Feb 2001 19:25:44 +0100
I am using xml.sax.writer to produce nicely indentex XML output from Python.
The xmlwriter is pretty good at indentation and formatting, but for some
tags, I would like to have whitespace preserved. I did not see a way
to tell this via the doctype info. Right now I am using the following:
class OutputWriter:
def __init__(self, fo=sys.stdout):
self.fo = fo
self.containers = []
self.docinfo = xml.sax.writer.XMLDoctypeInfo()
saxout = self.saxout = xml.sax.writer.PrettyPrinter(
self.fo, dtdinfo=self.docinfo, endtagindentation=-2)
# put a print here to see how (slow) output is generated.
# there should not be a visible delay between the message
# printed and the log output of the http server.
#print '### starting xml output'
saxout.startDocument()
def pcdata_tag(self, name, s):
s = '%s' % s
self.saxout.startElement(name)
self.saxout.characters(s, 0, len(s))
self.saxout.endElement(name)
def start_tag(self, name):
if not name in self.containers:
# needed to make pretty printing work (the pretty
# printer needs to know where whitespace is allowed
# in the output)
self.containers.append(name)
self.docinfo.add_element_container(name)
self.saxout.startElement(name, {})
def end_tag(self, name):
self.saxout.endElement(name)
def comment(self, text):
text = ' ' + text + ' '
self.saxout.comment(text, 0, len(text))
def close(self):
self.saxout.endDocument()
self.fo.flush()
This works the way I want only for short content (there is no whitespace
inserted before and after). Passing longer strings, possibly with
whitespace, to pcdata_tag will reformat, changing the internal and
external whitespace contained in my text.
Is there a way to do this with the current xmlwriter or is this
missing right now?
- Guenter