minidom and encoding problem

Timo Linna timo.spam.linna at iki.me.fi.not.invalid
Thu Jun 6 09:34:45 EDT 2002


"Ehab Teima" <ehab_teima at hotmail.com> wrote in message
news:17aafe08.0206051514.4af054d2 at posting.google.com...
> Hi,
>
> I know it's not possible to add an enconding attribute using writexml,
> so the generated document only has <?xml version="1.0"?>. Is there any
> way to get around this problem. I'd like to be able at least to parse
> the document while reading using the proper encoding, such as,
> encoding="ISO-8859-1". I'm only using minidom, any ideas how?

Thanks to Python, you could redefine minidom's writexml-function:

import xml.dom.minidom

def encoded_writexml(self, writer, indent="", addindent="", newl=""):
    writer.write('<?xml version="1.0" encoding="ISO-8859-1" ?>\n') # My
change
    for node in self.childNodes:
        node.writexml(writer, indent, addindent, newl)

xml.dom.minidom.Document.writexml = encoded_writexml

-- timo






More information about the Python-list mailing list