26

I am new to element tree,here i am trying to find the number of elements in the element tree.

from lxml import etree 
root = etree.parse(open("file.xml",'r'))

is there any way to find the total count of the elements in root?

har07
  • 88,338
  • 12
  • 84
  • 137
mariz
  • 509
  • 1
  • 7
  • 13

5 Answers5

29

Find all the target elements (there are some ways to do this), and then use built-in function len() to get the count. For example, if you mean to count only direct child elements of root :

from lxml import etree 
doc = etree.parse("file.xml")
root = doc.getroot()

result = len(root.getchildren())

or, if you mean to count all elements within root element :

result = len(root.xpath(".//*"))
har07
  • 88,338
  • 12
  • 84
  • 137
16

You don't have to load all the nodes into a list, you can use sum and lazily iterate:

from lxml import etree 
root = etree.parse(open("file.xml",'r'))
count = sum(1 for _ in root.iter("*"))
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • This is actually very useful for me since I need to count possible thousands or tenthousands of nodes, and I don't want to load all nodes in a list first. I'm not sure if that would lead to a memory 'leak', but just to be safe I think this a safer way. Do you think that this would be slower on very large trees? – Bram Vanroy Jun 11 '18 at 14:20
14

Another way to get the number of subelements:

len(list(root))
ThomasW
  • 16,981
  • 4
  • 79
  • 106
  • Note: `len(element)` would work as well. No need for `list()`, nor is this restricted to using it on the `root`. – payne Jul 04 '23 at 19:39
3

you can find the count of each element like this:

from lxml import objectify

file_root = objectify.parse('path/to/file').getroot()
file_root.countchildren()  # root's element count
file_root.YourElementName.countchildren()  # count of children in any element
SanD
  • 473
  • 7
  • 11
0
#  I used the len(list(  )) as a way to get the list of items in a feed, as I 
# copy more items I use the original len to break out of a for loop, otherwise
# it would keep going as I add items.  Thanks ThomasW  for that code.   

import xml.etree.ElementTree as ET

    def feedDoublePosts(xml_file, item_dup):
        tree = ET.ElementTree(file=xml_file)
        root = tree.getroot()
        for a_post in tree.iter(item_dup):
            goround = len(list(a_post))
            for post_children in a_post:
                if post_children != a_post:
                a_post.append(post_children)
                goround -= 1
                if goround == 0:
                    break
        tree = ET.ElementTree(root)
        with open("./data/updated.xml", "w") as f:
            tree.write(f)

    # ----------------------------------------------------------------------
    if __name__ == "__main__":
        feedDoublePosts("./data/original_appt.xml", "appointment")