这是我的XML结构:
<images>
<image>
<name>brain tumer</name>
<location>images/brain_tumer1.jpg</location>
<annotations>
<comment>
<name>Patient 0 Brain Tumer</name>
<description>
This is a tumer in the brain
</description>
</comment>
</annotations>
</image>
<image>
<name>brain tumer</name>
<location>img/brain_tumer2.jpg</location>
<annotations>
<comment>
<name>Patient 1 Brain Tumer</name>
<description>
This is a larger tumer in the brain
</description>
</comment>
</annotations>
</image>
</images>
我是Python新手,想知道是否根据评论检索位置数据:名称数据是否可行?换句话说,这是我的代码:
for itr1 in itemlist :
commentItemList = itr1.getElementsByTagName('name')
for itr2 in commentItemList:
if(itr2.firstChild.nodeValue == "Patient 1 Liver Tumer"):
commentName = itr2.firstChild.nodeValue
Loacation = it1.secondChild.nodeValue
任何建议还是我错过了什么? 提前谢谢。
答案 0 :(得分:1)
使用minidom
解析xml并不是一件好事,但这就是想法:
image
个节点找到Patient 1 Brain Tumer
评论的位置的示例:
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
打印:
img/brain_tumer2.jpg
答案 1 :(得分:1)
只是为了比较解决方案的简易性,以下是lxml的相同方法:
from lxml import etree
data = """
your xml goes here
"""
root = etree.fromstring(data)
print root.xpath('//image[.//comment/name = "Patient 1 Brain Tumer"]/location/text()')[0]
打印:
img/brain_tumer2.jpg
基本上,一行与六行。