Search XML Tags for Name Attribute: Python elementTree module
<p>I'm trying to parse an XML doc in Python using the <code>ElementTree</code> module. I'm trying to look for <code>Elements</code> that have an xml tag of <code>entry</code> and a <code>name</code> attribute within that tag that equals a certain <code>ipAddress</code>. Below is my code so far:</p>

<pre><code>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'
</code></pre>

<p>For reference, when I use the code <code>print child.tag, child.attrib</code> within the <code>for</code> loop, I get the following output:</p>

<pre><code>entry {'name': 'ipAddress'}
</code></pre>

<p>I need assistance searching for <code>entry</code> tags with a <code>name attribute</code> of <code>ipAddress</code></p>

<pre><code>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")
</code></pre>

python xml elementtree