root = ET.Element('bookstore')
tree = ET.ElementTree()
tree._setroot(root)
child0 = ET.Element('book',{'category':"COOKING"})
root.append(child0)
child00 = ET.Element('title',{'language':"English"})
child00.text='Everyday Italian'
child0.append(child00)
tree.write('createxml.xml','utf-8')
#encoding:utf-8'''
根据一个给定的XML Schema,使用DOM树的形式从空白文件生成一个XML。
'''
from xml.dom.minidom import Document
doc = Document() #创建DOM文档对象
bookstore = doc.createElement('bookstore') #创建根元素
bookstore.setAttribute('xmlns:xsi',"http://www.w3.org/2001/XMLSchema-instance")#设置命名空间
bookstore.setAttribute('xsi:noNamespaceSchemaLocation','bookstore.xsd')#引用本地XML Schema
doc.appendChild(bookstore)
############book:Python处理XML之Minidom################
book = doc.createElement('book')
book.setAttribute('genre','XML')
bookstore.appendChild(book)
tree.write('createxml.xml',xml_declaration=True, encoding='utf-8', method="xml")root = ET.Element('bookstore')
tree = ET.ElementTree()
tree._setroot(root)
child0 = ET.Element('book',{'category':"COOKING"})
root.append(child0)
child00 = ET.Element('title',{'language':"English"})
child00.text='Everyday Italian'
child0.append(child00)
tree.write('createxml.xml','utf-8')