python create xml
from xml.dom.minidom import Document
doc = Document()
root = doc.createElement("root")
card = doc.createElement("card")
card.setAttribute('id','2001000')
txt = doc.createElement("txt")
txtbody = doc.createTextNode("this is txt body")
txt.appendChild(txtbody)
card.appendChild(txt)
root.appendChild(card)
doc.appendChild(root)
print doc.toprettyxml(encoding='UTF-8')
# write to file
f=file('test.xml', 'w')
doc.writexml(f,encoding='UTF-8')
f.close()
doc = Document()
root = doc.createElement("root")
card = doc.createElement("card")
card.setAttribute('id','2001000')
txt = doc.createElement("txt")
txtbody = doc.createTextNode("this is txt body")
txt.appendChild(txtbody)
card.appendChild(txt)
root.appendChild(card)
doc.appendChild(root)
print doc.toprettyxml(encoding='UTF-8')
# write to file
f=file('test.xml', 'w')
doc.writexml(f,encoding='UTF-8')
f.close()
浙公网安备 33010602011771号