unicode + xml

Laurent Luce laurentluce49 at yahoo.com
Fri Sep 11 01:24:07 CEST 2009

Still doesn't work from Windows Japanese python (2.6.2) to Django Python 2.5.2. Works fine from Linux python 2.5.2 to Django Python 2.5.2.

Here is the flow:

- post xml utf-8 encoded data from Windows client to Django server
- On server: pass raw_post_data to minidom.parseString() ---> throws exception

Here is the code I use to post data:

url = mysite
req = urllib2.Request(url)
req.add_header('Content-Type', 'text/xml; charset=utf-8')
opener.open(req, data.encode('utf-8'))

data is the xml data
opener is a urllib2 opener I create when user logs in.

Here is the code I use to receive the data:

dom = minidom.parseString(request.raw_post_data)

default charset on django side is utf-8.

Please advise. Thanks.


----- Original Message ----
From: Stefan Behnel <stefan_ml at behnel.de>
To: python-list at python.org
Sent: Monday, September 7, 2009 11:50:28 PM
Subject: Re: unicode + xml

Laurent Luce wrote:
> Can someone confirm that the issue here is I need to encode the xml data using:
> # encode as UTF-8
> utf8_string = xml.encode( 'utf-8' )
> and then post it to the server.

Well, since you declared it to be UTF-8, it must be UTF-8 encoded.

However, your question seems to imply that you generate the XML manually
using string concatenation, which is a rather bad idea. Python has great
XML tools like ElementTree that help in generating and serialising XML
correctly (besides parsing, searching and other things).


More information about the Python-list mailing list