Python---xml

来源:互联网 发布:网络名词 编辑:程序博客网 时间:2024/05/20 04:48
'''import xml.etree.ElementTree as etreeElementTree is default python module, but it is slow, function is limitedso , we can import etree from lxml, it has the same api.'''from lxml import etree'''import lxml.etree as etreeit has the same function like above'''def getinfo1(path):tree = etree.parse(path)root = tree.getroot()print('========root========')print(root)print(len(root))print('=======tag============')print(root.tag)print('=========child===========')for child in root:print(child)print('======attribute=========')print(root.attrib)print('======attribute1========')print(root[0].attrib)print(root[1].attrib)print('======findall=========')all1=root.findall('{http://www.w3.org/2005/atom}title')print(all1)print(all1[0].find('{http://www.w3.org/2005/atom}des'))print(len(all1))'''findall, return listfind, return the first matched'''print('==========findall2==================')all2=tree.findall('//{http://www.w3.org/2005/atom}*[@href]')print(all2)'''// means all xml file, not just root or its children* means any item[@href] means include href as attribute'''def writeinfo1(path):root = etree.Element('root', lang='en')sub1=etree.SubElement(root, 'title', color='##FFFFFF')sub2= etree.SubElement(root, 'author', color = '##000000')sub2.set('color','##AAAAAA')sub1.set('type','text/html')sub1.text='the content of title will be added'print(etree.tostring(root, pretty_print=True))with open(path, mode='wb') as file:file.write(etree.tostring(root))if __name__ == '__main__':getinfo1('xml_util.xml')writeinfo1('xml_util2.xml')

0 0
原创粉丝点击