python读写xml文件

Deadly 提交于 2020-01-01 18:16:38

使用方法见:https://www.cnblogs.com/Zhouzg-2018/p/9926048.html

from  xml.dom.minidom import Document
import xml.etree.ElementTree as ET





#
# def writeXml(imgPath='',save_path="/home/gui/newXml.xml"):
#
#     #先简单实现下,考虑一个目标
#     doc=Document()
#     root=doc.createElement('annotation')
#     doc.appendChild(root)
#
#     node_1=doc.createElement('folder')
#     txt_1=doc.createTextNode('ImageSets')
#     node_1.appendChild(txt_1)
#     root.appendChild(node_1)
#
#     node_2=doc.createElement('name')
#     txt_2=doc.createTextNode('0001.jpg')
#     node_2.appendChild(txt_2)
#     root.appendChild(node_2)
#
#     node_3=doc.createElement('path')
#     txt_3=doc.createTextNode('/home/gui/mot2015/0001.jpg')
#     node_3.appendChild(txt_3)
#     root.appendChild(node_3)
#
#     node_4=doc.createElement('size')
#     node_41=doc.createElement('width')
#     txt_41=doc.createTextNode('100')
#     node_41.appendChild(txt_41)
#     node_42=doc.createElement('height')
#     txt_42=doc.createTextNode('100')
#     node_42.appendChild(txt_42)
#     node_4.appendChild(node_41)
#     node_4.appendChild(node_42)
#     root.appendChild(node_4)
#
#     node_object=doc.createElement('object')
#     node_5=doc.createElement('Xmin')
#     txt_5=doc.createTextNode('1')
#     node_5.appendChild(txt_5)
#     node_object.appendChild(node_5)
#     node_6=doc.createElement('Ymin')
#     txt_6=doc.createTextNode('1')
#     node_6.appendChild(txt_6)
#     node_object.appendChild(node_6)
#     node_7=doc.createElement('Xmax')
#     txt_7=doc.createTextNode('1')
#     node_7.appendChild(txt_7)
#     node_object.appendChild(node_7)
#     node_8=doc.createElement('Ymax')
#     txt_8=doc.createTextNode('1')
#     node_8.appendChild(txt_8)
#     node_object.appendChild(node_8)
#     root.appendChild(node_object)
#
#     with open(save_path,'w')as outFile:
#         doc.writexml(outFile,indent='\t', newl='\n', addindent='\t', encoding='gbk')
#
#         outFile.close()
#
# writeXml()


def generate(xmlFile=""):
    root=ET.Element('person')
    name=ET.SubElement(root,'name')
    name.text="xiaowang"
    age=ET.SubElement(root,'age')
    age.text="12"
    sex=ET.SubElement(root,"sex")
    sex.text="femal"
    tree=ET.ElementTree(root)

    tree.write(xmlFile,encoding="gbk",xml_declaration=True,method="xml",short_empty_elements=False)




  # <country name="Liechtenstein">
  #       <rank>1</rank>
  #       <year>2008</year>
  #       <gdppc>141100</gdppc>
  #       <neighbor name="Austria" direction="E"/>
  #       <neighbor name="Switzerland" direction="W"/>
  #   </country>

def test_1(xmlFile):
    root=ET.Element('data')
    country=ET.SubElement(root,'country',attrib={'name':"Liechtenstein"})
    rank=ET.SubElement(country,'rank')
    rank.text="1"
    year=ET.SubElement(country,'year')
    year.text="2008"
    gdppc=ET.SubElement(country,'gdppc')
    gdppc.text="141100"
    neighbor1=ET.SubElement(country,'neighbor1',attrib={"name":"Austria","direction":"E"})
    neighbor2=ET.SubElement(country,'neighbor2',attrib={"name":"Switzerland","direction":"W"})

    country = ET.SubElement(root, 'country', attrib={'name': "Singapore"})
    rank = ET.SubElement(country, 'rank')
    rank.text = "4"
    year = ET.SubElement(country, 'year')
    year.text = "2011"
    gdppc = ET.SubElement(country, 'gdppc')
    gdppc.text = "13545"
    neighbor1 = ET.SubElement(country, 'neighbor1', attrib={"name": "Malaysia", "direction": "N"})
    #neighbor2 = ET.SubElement(country, 'neighbor2', attrib={"name": "Switzerland", "direction": "W"})

    tree=ET.ElementTree(root)
    tree.write(xmlFile,encoding="utf-8")

def readXml(xmlFile=''):
    tree=ET.parse(xmlFile)
    root=tree.getroot()
    # for cy in root.iter('country'):
    #     if cy.attrib["name"]=='Singapore':
    #         rank=cy.find('rank')
    #         print('rank',rank.text)
    #         rank.text="10"
    #         year=cy.find('year')
    #         print('year',year.text)
    #         year.text="2013"
    country=ET.SubElement(root,'country',attrib={"name":"china"})
    rank = ET.SubElement(country, 'rank')
    rank.text = "133"
    year = ET.SubElement(country, 'year')
    year.text = "2016"
    gdppc = ET.SubElement(country, 'gdppc')
    gdppc.text = "135333"
    neighbor1 = ET.SubElement(country, 'neighbor1', attrib={"name": "shanghai", "direction": "E"})
    tree=ET.ElementTree(root)
    tree.write(xmlFile)

if __name__=="__main__":
    xmlfile="/home/gui/PycharmProjects/data_project/countryInfo.xml"
    # generate(xmlfile)
    # test_1(xmlfile)
    readXml(xmlfile)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!