搜索名称属性的XML标记:Python elementTree模块

时间:2016-09-21 18:20:41

标签: python xml elementtree

我尝试使用ElementTree模块在​​Python中解析XML文档。我正在尝试查找Elements的{​​{1}},其中entry的xml标记和该标记中的name属性等于某个ipAddress。以下是我目前的代码:

tree = ET.parse(fi2)
root = tree.getroot()
for child in root.iter('entry'): #looks through child elements for tag=entry
    #look for elements that have an attribute(name)='string'

作为参考,当我在print child.tag, child.attrib循环中使用代码for时,我得到以下输出:

entry {'name': 'ipAddress'}

我需要帮助搜索entry name attribute ipAddress

wdio.conf.js代码

2 个答案:

答案 0 :(得分:0)

import xml.etree.ElementTree as ET

tree = ET.parse(your_xml)
root = tree.getroot()
for child in root:
    if child.tag=='entry':
        for node in child:
            if node.attrib["name"]=='certain_ip':
                print("found")

答案 1 :(得分:0)

tree = ET.parse(fi2)
root = tree.getroot()
for child in root.iter('entry'): #looks through child elements for tag=entry
    #look for elements that have an attribute(name)='string'
    if ipAddress in child.get('name'):
        #condition is met. Execute some code. Do your thang