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'):
- 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?
- Also, is there a way to access the nth sub-child without having to go through the iter()?
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:
Direct access to element number #n: