views:

276

answers:

1

I'm using python 2.6.2's xml.etree.cElementTree to create an xml document:

import xml.etree.cElementTree as etree
elem = etree.Element('tag')
elem.text = (u"Würth Elektronik Midcom").encode('utf-8')
xml = etree.tostring(elem,encoding='UTF-8')

At the end of the day, xml looks like:

<?xml version='1.0' encoding='UTF-8'?>
<tag>W&#195;&#188;rth Elektronik Midcom</tag>

It looks like tostring ignored the encoding parameter and encoded 'ü' into some other character encoding ('ü' is a valid utf-8 encoding, I'm fairly sure).

Any advice as to what I'm doing wrong would be greatly appreciated.

+3  A: 

You're encoding the text twice. Try this:

import xml.etree.cElementTree as etree
elem = etree.Element('tag')
elem.text = u"Würth Elektronik Midcom"
xml = etree.tostring(elem,encoding='UTF-8')
John Millikin