您的位置:首页 > 编程语言 > Python开发

python use dom to write xml file

2013-07-11 10:59 573 查看
#encoding:utf-8
'''
write xml in dom style
'''

from xml.dom.minidom import Document

doc = Document() # new a DOM object

words = doc.createElement('words') # new a root element
words.setAttribute('xmlns:xsi',"http://www.w3.org/2001/XMLSchema-instance")#设置命名空间
doc.appendChild(words)

elem = doc.createElement('word')
elem.setAttribute('name','english')
words.appendChild(elem)

# or you could do like the following:
#elem = doc.createElement('word')
#words.appendChild(elem)
#text = doc.createElement('name')
#text_node = doc.createTextNode('english')
#text.appendChild(text_node)
#elem.appendChild(text)
print doc.toxml()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: