编程语言
首页 > 编程语言> > python 模块 xml

python 模块 xml

作者:互联网

1、xml包中的四个子包

1、dom包

2、SAX(simple API for XML)

3、parsers包

4、etree包

2、etree包解析xml

1、XML树和元素

1、遍历xml文件

 

 

 

示例:

import xml.etree.ElementTree as ET

tree = ET.parse("xmltest.xml")
root = tree.getroot()
#print(root)#<Element 'data' at 0x0000024CD0FF70E0>

nodelst = []
def xmlread(root_node, level):
    note = [level, root_node.tag, root_node.attrib, root_node.text]
    nodelst.append(note)
    child_node = list(root_node)
    # print(child_node)#[<Element 'country' at 0x000001B53D1C61D0>, <Element 'country' at 0x000001B53D1E9360>, <Element 'country' at 0x000001B53D1E94F0>]
    if len(child_node) == 0:
        return
    for child in child_node:
        xmlread(child, level + 1)

level = 1
xmlread(root, level)

for i in nodelst:
    print(i)

  

 

标签:xml,node,解析,python,XML,模块,ElementTree,root
来源: https://www.cnblogs.com/maiblogs/p/14800005.html