2012-11-01 9 views
3

我是这个论坛的新手。如何在xml.etree.elementree中使用XPath时使用Python 2.6.4获取父标签

我想从xml.etree.cElementTree检索数据。

我有以下代码

代码段

import xml.etree.cElementTree as ET 

xmldata =""" 
<pipeline> 
    <ep_150> 
     <stage name="lay" longname="layout" department="layout" process="production"> 
      <review name="R1" reviewer="sridhar reddy" role="supervisor" id="p1234"> 
      </review> 
     </stage> 
     <stage name="lip" longname="lipsync" department="lipsync" process="production"> 
      <review name="R2" reviewer="someone" role="supervisor" id="p2345"> 
      </review> 
     </stage> 
     <stage name="blk" longname="blocking" department="animation" process="production"> 
      <review name="R3" reviewer="sudeepth" role="supervisor" id="p4645" dependson='R1'> 
      </review> 
      <review name="R4" reviewer="chandu" role="director" id="p5678"> 
      </review> 
     </stage> 
     <stage name="pri" longname="primary" department="animation" process="production"> 
      <review name="R5" reviewer="sudeepth" role="supervisor" id="p4645" style="dep" > 
      </review> 
      <review name="R6" reviewer="sudeepth" role="bld_supervisor" id="p2556" style="dep"> 
      </review> 
     </stage> 
     <stage name="sec" longname="secondary" department="animation" process="production"> 
      <review name="R7" reviewer="sha" role="supervisor" id="p1234" style="dep"> 
      </review> 
      <review name="R8" reviewer="chandu" role="director" id="p5678"> 
      </review> 
     </stage> 
    </ep_150> 
</pipeline> 
""" 
root = ET.fromstring(xmldata) 

stages = root.findall("./ep_150/stage") 

print 'Stages in animation department....\n' 

for stage in stages: 

    if stage.attrib['department']=='animation': 
     print stage.attrib['name'] 

review = root.findall("./ep_150/stage/review")   

print '\n\nreviews for id=p4645\n' 

for rev in review: 

    if rev.attrib['id']=='p4645': 
     print (rev.attrib['name']) 

与上面的代码我得到的结果如下

阶段在动画部....

blk

个PRI

为ID = p4645

评论R3

R5

但我需要的输出为第二一半

reviewes为ID = p4645

BLK - R3

PRI - R5

即我需要的元素的父标签

回答

2

孩子们不知道他们的父母,但父母知道自己的孩子,所以你必须因此构造代码: -

stages = root.findall("./ep_150/stage")   

print '\n\nreviews for id=p4645\n' 

for stage in stages: 
    for rev in stage.findall('review'): 
     if rev.attrib['id']=='p4645': 
      print stage.attrib['name'], rev.attrib['name'] 

http://effbot.org/zone/element.htm#accessing-parents

无关的答案。如果你喜欢的话,你可以将这些内容移动到findall参数中: -

root = ET.fromstring(xmldata) 

stages = root.findall("./ep_150/stage[@department='animation']") 

print 'Stages in animation department....\n' 

for stage in stages: 
    print stage.attrib['name'] 

stages = root.findall("./ep_150/stage")   

print '\n\nreviews for id=p4645\n' 

for stage in stages: 
    for rev in stage.findall("review[@id='p4645']"): 
     print stage.attrib['name'], rev.attrib['name'] 
+0

你的建议是从父母那里检索孩子的工作,我尝试过并且工作正常。 – Rao

+0

但其他备用答案无效。它给了我下面的错误。 Traceback(最近一次调用最后一次): 文件“D:\ xmlTree.py”,第44行,在 stages = root.findall(“./ ep_150/stage [@ department ='animation']”) File“ C:\ Python26 \ lib \ xml \ etree \ ElementPath.py“,第198行,在findall return _compile(path).findall(element) 文件”C:\ Python26 \ lib \ xml \ etree \ ElementPath.py“ ,第176行,在_compile中 p =路径(路径) 文件“C:\ Python26 \ lib \ xml \ etree \ ElementPath.py”,行93,在__init__中 “预期路径分隔符(%s)”%(op或标记) SyntaxError:预期路径分隔符([) – Rao

+0

糟糕。它工作在2.7,我没有检查2.6。 – Himanshu