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()
posted @ 2012-05-26 19:54  ajaxj  阅读(175)  评论(0)    收藏  举报