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')

 


posted @ 2015-12-28 20:13  xfei.zhang  阅读(185)  评论(0编辑  收藏  举报