minidom xml & non ascii / unicode & files

"Martin v. Löwis" martin at
Sat Aug 6 20:15:35 CEST 2005

> so what i understood of all this, is that once you're using unicode
> objects you're safe !
> At least as long as you don't use statements or operators that will
> implicitely try to convert the unicode object back to bytestring using
> your default encoding (ascii) which will most certainly result in codec
> Errors...


> Also, minidom seems to use unicode object what was not really documented
> in the python 2.3 doc i've read about it..

It might be somewhat hidden:

"DOMString defined in the recommendation is mapped to a Python string or
Unicode string. Applications should be able to handle Unicode whenever a
string is returned from the DOM."
"The type DOMString maps to Python strings. xml.dom.minidom supports
either byte or Unicode strings, but will normally produce Unicode
strings. Values of type DOMString may also be None where allowed to have
the IDL null value by the DOM specification from the W3C."

In principle, you should fill Unicode strings into DOM trees all the
time, but it will work with byte strings as well as long as they are

> As a matter of fact using the following sequence will most certainly fail :
> f ='utf8codecs.xml', 'w', 'utf-8')
> f.write(dom.toxml(encoding="utf-8"))
> f.close()

Correct. A codecs.StreamWriter expects Unicode objects, whereas toxml
returns byte strings (atleast if you pass an encoding - because of a
bug, it might return a Unicode string otherwise)

> then again maybe this will work, i just thought of it..
> f ='utf8codecs.xml', 'w', 'utf-8')
> f.write(dom.toxml())
> f.close()

Yeah, toxml() returned Unicode because of a bug - but for backwards
compatibility, this cannot be changed. People should explicitly pass
an encoding.

> The next important thing is to make sure to use functions and objects
> that support unicode all the way, like minidom seems to do..

Indeed, there are still many functions in the standard library which
don't work with Unicode strings, but should. Some functions, of course,
are only meaningful for byte strings (like networking API).


More information about the Python-list mailing list