New submission from GPU Group <gpugr...@gmail.com>:

Py32 > Lib > xml.dom.minidom  > usage feedback > overrides
I like the minidom. I use it -and over-ride the Element.writexml() and 
_write_data() methods for a few of my Blender.org B259 personal export scripts 
to formats:

.x3d - a www.web3d.org public format, an xml-like version of vitual reality 
.vrml, except it wants single quotes with double quotes inside:
'"EXAMINE" "ANY"'. It's an attribute heavy format.

.kml - an earth.google.com format, element heavy. By default minidom puts extra 
lines backslash n so the following comes out on 3 lines:
                <description>
                        1909 era from 1891 to 1930
                </description>
and I want something more compact on one line:
<description>1909 era from 1891 to 1930</description>

In both cases I over-ride the minidom:
save_original_writexml = xml.dom.minidom.Element.writexml 
xml.dom.minidom.Element.writexml = writexml_pluto
... do my exporting ...
# other registered scripts share the minidom, so restore it
xml.dom.minidom.Element.writexml = save_original_writexml

I'm happy with this, as long as I remember to update my minidom overrides with 
new py versions. I mention it in case I'm using it wrong or in case you like 
what I'm doing and want to adopt or spread the word.

more...
kml over-ride for compact element-heaviness:
    def _write_data_orig(writer, data):
        "Writes datachars to writer."
        if data:
            data = data.replace("&", "&amp;").replace("<", "&lt;"). \
                        replace("\"", "&quot;").replace(">", "&gt;")
            writer.write(data)
          
            
    def writexml_plutokml(self, writer, indent="", addindent="", newl=""):
        # indent = current indentation
        # addindent = indentation to add to higher levels
        # newl = newline string
        writer.write(indent+"<" + self.tagName)

        attrs = self._get_attributes()
        a_names = sorted(attrs.keys())

        for a_name in a_names:
            writer.write(" %s=\"" % a_name)
            _write_data_orig(writer, attrs[a_name].value)
            writer.write("\"")
        if self.childNodes:
            #pluto- writer.write(">%s"%(newl))
            writer.write(">")  # pluto+
            tx = False  # pluto+
            k=0  # pluto+
            for node in self.childNodes:
              k=k+1  # p+
              if node.nodeType == Node.TEXT_NODE:  # p+
                node.writexml(writer,"","","")      # p+
                tx = True           # p+
              else:    # p+
                if k == 1: writer.write(newl) # p+
                node.writexml(writer, indent + addindent, addindent, newl)
            if tx:  # p+
                writer.write("%s</%s>%s" % ("",self.tagName,newl))  # p+
            else:  # p+
                writer.write("%s</%s>%s" % (indent, self.tagName, newl))
        else:
            writer.write("/>%s"%(newl))

x3d over-ride for apos (versus quote) attribute delimeters:
    def _write_data_pluto(writer, data):
        "Writes datachars to writer."
        if data:
            data = data.replace("&", "&amp;").replace("<", "&lt;"). \
                        replace("'", "&apos;").replace(">", "&gt;")   # pluto: 
apos instead of quot
            writer.write(data)

    def writexml_pluto(self, writer, indent="", addindent="", newl=""):
        # indent = current indentation
        # addindent = indentation to add to higher levels
        # newl = newline string
        writer.write(indent+"<" + self.tagName)

        attrs = self._get_attributes()
        a_names = sorted(attrs.keys())

        for a_name in a_names:
            writer.write(" %s='" % a_name)  # pluto, orig: writer.write(" 
%s=\"" % a_name)
            _write_data_pluto(writer, attrs[a_name].value)
            writer.write("'")  # pluto, orig: writer.write("\"")
        if self.childNodes:
            writer.write(">%s" % (newl))
            for node in self.childNodes:
                node.writexml(writer, indent+addindent, addindent, newl)
            writer.write("%s</%s>%s" % (indent, self.tagName, newl))
        else:
            writer.write("/>%s"%(newl))

----------
components: XML
messages: 143232
nosy: GPU.Group
priority: normal
severity: normal
status: open
title: py32 > Lib > xml.minidom > usage feedback > overrides
type: behavior
versions: Python 3.2

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue12863>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to