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 38 39 40 41 42 43 44
| # -*- coding: utf-8 -*-
from xml.dom import minidom
import tempfile
import os
balise=3
print"un test :"
DOMimpl = minidom.getDOMImplementation()
xmldoc = DOMimpl.createDocument(None,"TestDom", None)
doc_root = xmldoc.documentElement
j="Pentium M"
for i in range(balise):
node = xmldoc.createElement("Computer")
node.setAttribute('Processor', j)
node.setAttribute('Memory', "512MB")
doc_root.appendChild(node)
###
element = xmldoc.createElement('SousElement')
element.setAttribute('elem1', "1")
element.setAttribute('elem2',"2")
node.appendChild(element)
nodeList = doc_root.childNodes
for node in nodeList:
print node.toprettyxml()
tempfile = tempfile.mktemp()
print "tempfile", "=>", tempfile
file = open(tempfile, "w+b")
file.write(xmldoc.toxml())
try:
os.remove(tempfile)
except OSError:
print"fichier n'est pas supprimé" |
Partager