Python---xml

'''import xml.etree.ElementTree as etree
ElementTree is default python module, but it is slow, function is limited
so , we can import etree from lxml, it has the same api.
'''
from lxml import etree
'''import lxml.etree as etree
it 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 list
    find, 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')

 


转载于:https://www.cnblogs.com/xfei-zhang/p/5086837.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值