2014-03-28 33 views
1

這裏與條件解析是我的XML結構:XML樹在Python

<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 

任何建議,還是我在這裏失去了財產以後? 預先感謝您。

回答

1

minidom解析XML是不是樂趣可言,但這裏的想法:

  • 迭代所有image節點
  • 每個節點上,檢查註釋/ name文本
  • 如果文本匹配,獲得位置節點的文本

例,對於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 
+0

令人印象深刻!謝謝。 –

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 

基本上,一條線對六條。