Parsing xml with minidom
isn't fun at all, but here's the idea:
- iterate over all
image
nodes - for each node, check comment/name text
- if the text matches, get the location node's text
Example that finds location for Patient 1 Brain Tumer
comment:
import xml.dom.minidom
data = """
your xml goes here
"""
dom = xml.dom.minidom.parseString(data)
for image in dom.getElementsByTagName('image'):
comment = image.getElementsByTagName('comment')[0]
comment_name_text = comment.getElementsByTagName('name')[0].firstChild.nodeValue
if comment_name_text == 'Patient 1 Brain Tumer':
location = image.getElementsByTagName('location')[0]
print location.firstChild.nodeValue
prints:
img/brain_tumer2.jpg