Bonjour,
Pour l'élaboration d'un programme j'ai besoin d'écrire et de lire des fichiers xml.

Pour ce faire j'utilise le code suivant
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/python
#-*- coding:utf8 -*-
 
 
import xml.dom.minidom
import codecs
 
 
class XMLTest:
	def __init__(self):
		pass
 
	def load(self):
		from xml.dom.minidom import parse
		fileXML = codecs.open("test.xml", 'r+','utf-8')
		doc = parse(fileXML)
		for testEl in doc.documentElement.getElementsByTagName("test"):
			if testEl.nodeType == testEl.ELEMENT_NODE:
				try:
					print testEl.childNodes[0].nodeValue
				except:
					print 'Pb de tag '
 
	def save(self):
		doc = xml.dom.minidom.Document()
		rootElem = doc.createElement("tests")
		doc.appendChild(rootElem)
		tmpNode = doc.createElement("test")
		tmpNode.appendChild(doc.createTextNode("héhé"))
		rootElem.appendChild(tmpNode)
		fp = open("test.xml","w")
		doc.writexml(fp, "", "", "", "UTF-8")
 
if __name__ == "__main__":
	x = XMLTest()
	x.save()
	x.load()
Ce n'est en fait pas dutout le code utilisé mais un code qui produit la même erreur

lorsque je lance ce code il me fait une erreur

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
Traceback (most recent call last):
  File "links2.py", line 37, in <module>
    x.load()
  File "links2.py", line 16, in load
    doc = parse(fileXML)
  File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/xml/dom/minidom.py", line 1913, in parse
    return expatbuilder.parse(file)
  File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/xml/dom/expatbuilder.py", line 928, in parse
    result = builder.parseFile(file)
  File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/xml/dom/expatbuilder.py", line 207, in parseFile
    parser.Parse(buffer, 0)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 52: ordinal not in range(128)
Si quelqu'un voit a quoi c'est du ce serait vraiment sympathique

merci beaucoup