我尝试使用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
代码
答案 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