Bonjour,

je suis a l'heure actuelle en train de monter en compétence sur python et j'ai besoin de générer des fichiers XML.

J'utilise pour ceci 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
from xml.dom import minidom
import codecs
xml = minidom.Document()
speechMacrosElement = xml.createElement('speechMaccros')
commentElement = xml.createElement('command')
listenFor = xml.createElement('listenFor')
xlisten = xml.createTextNode("tata")
listenFor.appendChild(xlisten)
speak = xml.createElement('speak')
xspeak = xml.createTextNode("toto")
speak.appendChild(xspeak)
commentElement.appendChild(listeFor)
commentElement.appendChild(listenFor)
commentElement.appendChild(speak)
speechMacrosElement.appendChild(commentElement)
outFile=codecs.open('D:\\xmlout.xml','w','UTF-16')
speechMacrosElement.writexml(outfile,'','UTF-16','\n')
outfile.close()
et voici se que cela me retourne :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
<speechMaccros>
UTF-16<command>
UTF-16UTF-16<listenFor>
UTF-16UTF-16UTF-16tata
UTF-16UTF-16</listenFor>
UTF-16UTF-16<speak>
UTF-16UTF-16UTF-16toto
UTF-16UTF-16</speak>
UTF-16</command>
</speechMaccros>
J'avoue avoir suivi plus ou moins des instruction trouvé ici et là. Mais je n'obtien pas ce que je souhaite.

Le résultat idéal pour moi serai quelque chose de cette forme:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
<?xml version="1.0" encoding="UTF-16"?>
<speechMacros>
  <command>
    <listenFor>tata</listenFor>
    <speak>listen tata</speak>
  </command>
</speechMacros>
Je bloque c'est pourquoi je fait appel a votre aide pour me donner une piste que je n'aurais peut être pas exploiter.

Merci.