This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: make minidom.toxml() encoding argument useful
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.2
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: BreamoreBoy, eric.araujo, janssen
Priority: normal Keywords: easy

Created on 2008-06-10 18:12 by janssen, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg67909 - (view) Author: Bill Janssen (janssen) * (Python committer) Date: 2008-06-10 18:11
Right now, the encoding argument added to
xml.dom.minidom.DOMObject.toxml() in Python 2.3 seems fairly useless. 
It has to be UTF-8.  But a one-line change to the implementation of
toprettyxml would make it useful; instead of the encoding error method
being "strict", make it "xmlcharrefreplace".  So change

    writer = codecs.lookup(encoding)[3](writer)

to

    writer = codecs.lookup(encoding)[3](writer, "xmlcharrefreplace")
msg67910 - (view) Author: Bill Janssen (janssen) * (Python committer) Date: 2008-06-10 18:22
The method "toxml" is actually on xml.dom.minidom.Node.
msg107649 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2010-06-12 13:06
Is this still relevant for 3.2?
msg111607 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2010-07-26 13:03
This is how toprettyxml looks in 3.1/2 which seems to meet the OP's need, I'll close in a few days time unless someone objects.

def toprettyxml(self, indent="\t", newl="\n", encoding=None):
    # indent = the indentation string to prepend, per level
    # newl = the newline string to append
    use_encoding = "utf-8" if encoding is None else encoding
    writer = codecs.getwriter(use_encoding)(io.BytesIO())
    if self.nodeType == Node.DOCUMENT_NODE:
        # Can pass encoding only to document, to put it into XML header
        self.writexml(writer, "", indent, newl, encoding)
    else:
        self.writexml(writer, "", indent, newl)
    if encoding is None:
        return writer.stream.getvalue().decode(use_encoding)
    else:
        return writer.stream.getvalue()
msg113244 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2010-08-08 09:38
Closing as no response to msg111607.
History
Date User Action Args
2022-04-11 14:56:35adminsetgithub: 47325
2010-08-08 09:38:24BreamoreBoysetstatus: pending -> closed
resolution: out of date
messages: + msg113244
2010-07-26 13:03:55BreamoreBoysetstatus: open -> pending
nosy: + BreamoreBoy
messages: + msg111607

2010-06-12 13:06:34eric.araujosetversions: + Python 3.2, - Python 3.3
2010-06-12 13:06:19eric.araujosetnosy: + eric.araujo

messages: + msg107649
versions: + Python 3.3, - Python 2.6, Python 3.0
2008-06-10 18:22:56janssensettype: enhancement
messages: + msg67910
2008-06-10 18:12:02janssencreate