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 8610789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:12:38+00:00 2026-06-12T04:12:38+00:00

I am XML file, there are 10 sub-children (same hierarchy) of root, having the

  • 0

I am XML file, there are 10 sub-children (same hierarchy) of root, having the name ‘testCase’

I am not able to figure out the following:
Firstly, I am doing the following to get all the sub-children:

for testCase in root.iter('testCase'):
  1. I need to get some attribute from the last sub-child ‘testCase’. But how do i know that it is the last ‘testCase’. Is there a way to count them?
  2. Also, is there a way to access the nth sub-child without having to go through the iter()?
  • 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-12T04:12:39+00:00Added an answer on June 12, 2026 at 4:12 am

    Concerning this type of manipulations, you should use XPath, which is a common and simple way to browse XML trees. I don’t think the standard Python ElementTree support XPath, but lxml does (very commonly used as well), here is an example:

    Get last child:

    >>> text = """<Root>
        <Child name="child1" />
        <Child name="child2" />
        <Child name="child3" />
        <Child name="child4" />
        <Child name="child5" />
    </Root>"""
    >>> from lxml import etree
    >>> root = etree.fromstring(text)
    >>> last_tag = root.xpath('/Root/Child[last()]')[0]
    >>> last_tag.attrib['name']
    'child5'
    

    Direct access to element number #n:

    >>> tag3 = root.xpath('/Root/Child[3]')[0]
    >>> tag3.attrib['name']
    'child3'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

say there is an xml file, which not created by me, with a known
Is there any way to strip elements out of a web.xml file using ANT?
I created a method to unmarshall my xml (item.xml) file .But if there are
Is there any PHP script to convert XML file to DTD. I tried a
Is there a way to validate an XML file against a DTD with Qt's
When reading a XML file with linq to XML using a XDocument and there
I have created XML file,but I can't view it/output it.I know there is no
Is there any ways to do strings concatination in a `build.xml file? Concretely, suppose
Is there a way to read off from an [external] xml (an xml file
I'm playing with an imported XML file and using XMLDocument I'm wondering if there

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.