2016-09-21 69 views
0

我正在尝试使用ElementTree模块解析Python中的XML文档。我正在寻找Elements,该标签中的xml标签为entryname属性为ipAddress。下面是我到目前为止的代码:搜索名称属性的XML标签:Python elementTree模块

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' 

作为参考,当我使用for循环内的代码print child.tag, child.attrib,我得到下面的输出:

entry {'name': 'ipAddress'} 

我需要帮助寻找entry标签用的ipAddress

回答

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") 
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 
name attribute