ASCII decoding error with xml.dom.minidom

Gustaf Liljegren gustafl at algonet.se
Sat Jun 16 01:06:55 EDT 2001


Still got a problem with encoding/decoding errors when working with 
xml.dom.minidom. I have run into something I didn't ask for. The DOM module 
continues to output everything as Unicode strings, even if the file is a 
typical 'plain text' XML file with an ISO 8859-1 encoding attribute in the 
XML declaration!

The input data comes from two directions: one XML file, where the input 
takes the form of Unicode strings as described above, and a mailbox file, 
in Latin 1. Content from these two sources should be mixed together in an 
XML output file.

Now everything works (without any encoding/decoding in either direction) 
until it's time to save it in XML. This gives an error:

f = open('e:/test/catalog.xml', 'w')
f.write(doc.toxml())  # Error!
f.close()

And here's the full message:

Traceback (most recent call last):
  File "etest.py", line 59, in ?
    f.write(doc.toxml())
  File "E:\python\_xmlplus\dom\minidom.py", line 87, in toxml
    return writer.getvalue()
  File "e:\python\lib\StringIO.py", line 160, in getvalue
    self.buf += ''.join(self.buflist)
UnicodeError: ASCII decoding error: ordinal not in range(128)

Ideally, I'd like the output XML file in Latin 1. I wonder if there's an 
easy way to decode everything in the DOM object to Latin 1, so that this 
won't happen?

Regards,

Gustaf Liljegren



More information about the Python-list mailing list