3

I am newbie to this forum.

I am trying to retrieve data from xml.etree.cElementTree.

i have the following code

Code Snippet

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

with the above code i am getting the result as below

Stages in animation department....

blk

pri

sec

reviews for id=p4645

R3

R5

But i need the output for second half as

reviewes for id=p4645

blk - R3

pri - R5

i.e, i need the parent tag of the element

1 Answer 1

2

The children don't know about their parents but the parents know about their children so you have to structure your code accordingly :-

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

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

Unrelated to the answer. You can move those if's inside the findall argument if you like :-

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']
Sign up to request clarification or add additional context in comments.

5 Comments

your suggestion for retrieving child from parent its working that i tried and it worked.
but the other alternate answer did not work. it gave me following error. Traceback (most recent call last): File "D:\xmlTree.py", line 44, in <module> stages = root.findall("./ep_150/stage[@department='animation']") File "C:\Python26\lib\xml\etree\ElementPath.py", line 198, in findall return _compile(path).findall(element) File "C:\Python26\lib\xml\etree\ElementPath.py", line 176, in _compile p = Path(path) File "C:\Python26\lib\xml\etree\ElementPath.py", line 93, in init "expected path separator (%s)" % (op or tag) SyntaxError: expected path separator ([)
i think Xpath are not completly supported in python 2.6.4. its is supported in 2.7 onwards.[link[(docs.python.org/2.7/library/…)
is there any alternate to use xPath in 2.6, bcoz i tried it and its much more useful. but i cannot change my ver to 2.7
There are no others in the Python standard library. You could try lxml.de/installation.html but that may get difficult on win

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.