2016-09-21 17 views
0

ElementTreeモジュールを使用してPythonでXML文書を解析しようとしています。私は、特定のipAddressに等しいそのタグ内のentrynameという属性を持つElementsを探しています。以下は私のコードは、これまでのところです:私はforループ内のコードprint child.tag, child.attribを使用する場合名前属性の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' 

、私は次のような出力が得られます。

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
関連する問題