Message143232
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("&", "&").replace("<", "<"). \
replace("\"", """).replace(">", ">")
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("&", "&").replace("<", "<"). \
replace("'", "'").replace(">", ">") # 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)) |
|
Date |
User |
Action |
Args |
2011-08-30 18:50:58 | GPU.Group | set | recipients:
+ GPU.Group |
2011-08-30 18:50:58 | GPU.Group | set | messageid: <1314730258.77.0.934592208452.issue12863@psf.upfronthosting.co.za> |
2011-08-30 18:50:58 | GPU.Group | link | issue12863 messages |
2011-08-30 18:50:57 | GPU.Group | create | |
|