如何在Python2.6.4中使用xml.etree.elementree中的XPath时获取父标记

如何在Python2.6.4中使用xml.etree.elementree中的XPath时获取父标记,python,xml.etree,Python,Xml.etree,我是这个论坛的新手 我正在尝试从xml.etree.cElementTree检索数据 我有以下代码 代码片段 import xml.etree.cElementTree as ET xmldata =""" <pipeline> <ep_150> <stage name="lay" longname="layout" department="layout" process="production"> <r

我是这个论坛的新手

我正在尝试从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'])
将xml.etree.cElementTree作为ET导入
xmldata=”“”
"""
root=ET.fromstring(xmldata)
阶段=根findall(“/ep_150/阶段”)
打印“动画部门中的阶段…”\n
对于分期付款:
如果stage.attrib['department']=='animation':
打印stage.attrib['name']
审查=根芬德尔(“/ep_150/阶段/审查”)
打印“\n\n id=p4645的视图\n”
审查中的修订版:
如果版本属性['id']='p4645':
打印(attrib修订版['name'])
使用上面的代码,我得到的结果如下

动画系的舞台

黑色

pri

id=p4645的审查

R3

R5

但我需要下半年的输出

id=p4645的审查

blk-R3

pri-R5


i、 e,我需要元素的父标记

子元素不知道他们的父元素,但父元素知道他们的子元素,因此您必须相应地构造代码:-

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']

与答案无关。如果愿意,可以在findall参数中移动这些if:-

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']

你关于从父母那里取回孩子的建议我试过了,它奏效了。但另一个备选答案不起作用。它给了我以下的错误。回溯(最后一次调用):文件“D:\xmlTree.py”,第44行,stages=root.findall(“/ep_150/stage[@department='animation']”),文件“C:\Python26\lib\xml\etree\ElementPath.py”,第198行,在findall返回编译(路径)文件中。findall(元素)文件“C:\Python26\lib\xml\etree\ElementPath.py”,第176行,在编译p=path(路径)文件中“C:\Python26\lib\xml\etree\ElementPath.py”,第93行,在init“预期路径分隔符(%s)”%(op或tag)语法错误:预期路径分隔符([)我认为Xpath在python 2.6.4中不完全受支持。它在2.7以后的版本中受支持。[链接[()在2.6中是否有其他方法可以使用xPath,因为我已经尝试过了,而且它更有用。但是我不能将我的版本更改为2.7。Python标准库中没有其他版本。您可以尝试,但在win中可能会遇到困难