[XML-SIG] XmlWriter fix?

Jeff.Johnson@stn.siemens.com Jeff.Johnson@stn.siemens.com
Mon, 13 Jul 1998 11:03:00 -0400


The following is a subclass of XmlWriter that I think should be
incorporated back into XmlWriter.  Without this, if I add a tag to
new_line_before_start and new_line_before_end and the tag is empty
"<tag/>", I get two newlines before the tag.  This change to the code
detects that case and only writes one newline.  The problem is due to the
fact that the start tag and the end tag are the same tag.


"""Just a temporary fix to XmlWriter"""
from xml.dom.core import *
from xml.dom.writer import XmlWriter

class MyXmlWriter(XmlWriter):
     "Just a hack to stop empty tags from printing two newlines"
     def endElement(self, element):
          assert element.NodeType == ELEMENT

          s = ''
          if element.tagName in self.empties :
               pass
          elif len(element.getChildren()) == 0 and self.xml_style_endtags:
               if element.tagName in self.newline_before_end and \
                    element.tagName not in self.newline_before_start:
                         self.stream.newLine()
               self.stream.write(s)
               if element.tagName in self.newline_after_end and \
                    element.tagName not in self.newline_after_start:
                         self.stream.newLine()
          else:
               s = s + '</%s>' % self.map_tag(element.tagName)

               if element.tagName in self.newline_before_end:
                    self.stream.newLine()
               self.stream.write(s)
               if element.tagName in self.newline_after_end:
                    self.stream.newLine()