classification
Title: py32 > Lib > xml.minidom > usage feedback > overrides
Type: behavior Stage: resolved
Components: XML Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: GPU.Group, eric.araujo
Priority: normal Keywords:

Created on 2011-08-30 18:50 by GPU.Group, last changed 2011-09-02 16:58 by eric.araujo. This issue is now closed.

Messages (2)
msg143232 - (view) Author: GPU Group (GPU.Group) Date: 2011-08-30 18:50
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))
msg143416 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2011-09-02 16:58
For general discussion or questions about usage, you are welcome to use mailing lists such as python-list and xml-sig.  This tracker is dedicated to bug reports and feature requests, and unless I’m misreading your report contains neither, so I’m closing it.  Have fun with Python and XML!
History
Date User Action Args
2011-09-02 16:58:51eric.araujosetstatus: open -> closed

versions: - Python 3.2
nosy: + eric.araujo

messages: + msg143416
resolution: not a bug
stage: resolved
2011-08-30 18:50:58GPU.Groupcreate