Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8816663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:40:16+00:00 2026-06-14T04:40:16+00:00

I am newbie to this forum. I am trying to retrieve data from xml.etree.cElementTree.

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:40:17+00:00Added an answer on June 14, 2026 at 4:40 am

    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']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to extract data from this String: Hello there. Blah blahblah blah
I am newbie to jQuery, can someone explain what this code does: $(#currency form).submit(function(e)
I'm trying to solve this newbie puzzle: I've created this function: def bucket_loop(htable, key):
Firstly: I am totally a newbie for this kind of work. I have a
I'm a jQuery newbie as of this morning, and this is the code I've
I'm a newbie in applescript. I was trying to learn it from various sources,
I'm newbie on this, I'm trying to apply the shake effect to a dialog
I'm a newbie Rails developer who is getting the following error when trying to
Complete rails newbie trying to get started. I have two classes, Ingredient, and Unit.
I'm a django newbie. I have this model: class Item(models.Model): name = models.CharField(max_length=255) quantity

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.