python解析xml文件

来源:互联网 发布:淘宝联盟app推广教程 编辑:程序博客网 时间:2024/06/05 05:35

ET.find(path[, namespaces=D])

This method is used to find a specific single element in the document. It is essentially equivalent to calling the .find()method on the document's root element;

<country name="chinese">

>>> for child in root:...   print child.tag, child.attrib
child.tag 是标签项(country), child.attrib 是标签的名字(chinese)

    root = ElementTree.fromstring(text)



    # 获取element的方法  
    # 1 通过getiterator   
    infra_node = root.getiterator("virtual-infrastructure")
    #for node in lst_node:  
     #   print_node(node)  


    # 2通过 getchildren  Deprecated since version 2.7: Use list(elem) or iteration
    lst_node_child = infra_node[0].getchildren()[0]
    print_node(lst_node_child)


    # 3 .find方法  
    node_find = root.find('address')
    #print_node(node_find)  


    #4. findall方法  
    node_findall = root.findall("virtual-infrastructure/vm")[0]

    #print_node(node_findall) 


for elem in tree.iter(tag='branch'):...   print elem.tag, elem.attrib

0 0
原创粉丝点击