Counting entire xml file rather than just selected elements



i am trying to use elementtree to count some elements in an XML file. here is the structure of the XML:



<server>
<client>
<content>
<files>
<index name/>
<index name/>
<index name/>
</files>
</content>
</client>
</server>
<server>
<client>
<content>
<files>
<index name/>
<index name/>
<index name/>
</files>
</content>
</client>
</server>


So what i want to do is count the index elements. the issue i am having is i need to look at counting the amount of index elements within each files container.


when i search for index it counts the entire xml file rather than the just the ones within the files tag.


Here's my code:



tree = ET.parse(filepath)
root = tree.getroot()

test = 0
for i in root.iter('files'):
for i in root.iter('index'):
test += 1
print test


I'd appreciate any help.


I'm running python 2.7.8 just FYI like


No comments:

Post a Comment