Python operates the xml method, parses xml, creates xml, and creates xml in the form of a list dictionary

Parse xml

#coding=utf-8
from xml.dom.minidom import parse

print('-----------')
DOMTree = parse(r'E:\test.xml') #打开xml文件
booklist=DOMTree.documentElement #获取根节点
print(DOMTree.toxml()) #打印文件内容
print(booklist.toxml()) #打印文件内容
booklist.hasAttribute("type") #判断booklist节点是否包含type属性
booklist.getAttribute("type") #获取booklist节点的type属性的值
books=booklist.getElementsByTagName("book") #获取booklist节点下面的所有book节点
books[0].childNodes #获取book节点下所有的子节点,该子节点列表中包含了换行符[<DOM Text node "'\n\t\t'">, <DOM Element: title at 0x308cec8>, <DOM Text node "'\n\t\t'">, <DOM Element: author at 0x308cf10>, <DOM Text node "'\n\t\t'">, <DOM Element: pageNumber at 0x308cf58>, <DOM Text node "'\n\t'">]

d={
    
    }
for i in range(1,6,2): #该处从1开始,6结束,步长为2,时为了避开换行符
    tag_name=books[1].childNodes[i].tagName #获取books[1]下面第i个子节点标签
    d[tag_name]=books[1].childNodes[i].childNodes[0].data #获取books[1]下面第i个子节点标签的文本内容存入字典中
    
for k,v in d.items():
    print(k,v) #遍历打印字典
    
if books[0].hasChildNodes(): #判断books[0]下是否有子节点
    print('有子节点')

Create xml file

#coding=utf-8
import xml.dom.minidom

doc=xml.dom.minidom.Document() #创建一个xml对象
print(doc)
root=doc.createElement('company') #创建一个根节点
root.setAttribute('information',u'公司信息') #给根节点增加一个属性
doc.appendChild(root) #将root节点添加到文档中

gloryroad=doc.createElement('gloryroad') #给根节点创建子节点,此时该子节点还未挂载在根节点下面
name=doc.createElement('name') #子节点下再创建子节点
name.appendChild(doc.createTextNode(u'天利公司')) #给name节点添加文本内容
ceo=doc.createElement('ceo') #创建新节点
ceo.appendChild(doc.createTextNode('王总')) #给节点添加文本内容

gloryroad.appendChild(name) #将name节点挂载到gloryroad节点下
gloryroad.appendChild(ceo) #将ceo节点挂载到gloryroad节点下
root.appendChild(gloryroad) #将gloryroad节点挂载到根节点下
print(doc.toxml())

fp=open('e:\\company.xml','w',encoding='utf-8') #创建并打开文件
doc.writexml(fp,indent='',addindent='\t',newl='\n',encoding='utf-8')
#将xml对象写入文件中,其中indent根节点缩进方式,addindent子节点缩进方式,newl新行的换行方式,encoding编码方式
fp.close

Create xml with a list dictionary

#coding=utf-8
import xml.dom.minidom

doc=xml.dom.minidom.Document() #创建一个xml对象
print(doc)
root=doc.createElement('company') #创建一个根节点
root.setAttribute('information',u'公司信息') #给根节点增加一个属性
doc.appendChild(root) #将root节点添加到文档中

managerList=[{
    
    'name':'小明','age':18,'sex':'男'},
             {
    
    'name':'小红','age':17,'sex':'女'},
             {
    
    'name':'小王','age':19,'sex':'男'}]
             
for i in managerList:
    nodeManager=doc.createElement('Manager') #给根节点创建子节点
    nodeName=doc.createElement('name') #给Manager创建子节点
    nodeName.appendChild(doc.createTextNode(str(i['name']))) #给name节点添加文本内容
    nodeAge=doc.createElement('age')
    nodeAge.appendChild(doc.createTextNode(str(i['age'])))
    nodeSex=doc.createElement('sex')
    nodeSex.appendChild(doc.createTextNode(str(i['sex'])))
    nodeManager.appendChild(nodeName) #将子节点挂载在父节点上
    nodeManager.appendChild(nodeAge)
    nodeManager.appendChild(nodeSex)
    root.appendChild(nodeManager)
    
fp=open('e:\\company1.xml','w',encoding='utf-8')
doc.writexml(fp,indent='',addindent='\t',newl='\n',encoding='utf-8')
fp.close

Guess you like

Origin blog.csdn.net/weixin_44123630/article/details/113786394