How to find the number of elements in element tree in python?
Asked Answered
E

5

27

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?

Emmer answered 1/7, 2016 at 6:41 Comment(0)
H
30

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(".//*"))
Hallowmas answered 1/7, 2016 at 6:59 Comment(3)
result = len(root.xpath(".//*")) this is what exactly i'm looking for..thank u,..Emmer
Unfortunately, getchildren was deprecated in python 2.7.Adelbert
root.getchildren() -> list(root) as per the docs and you can simply do len(root)Machzor
B
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("*"))
Blank answered 1/7, 2016 at 23:25 Comment(1)
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?Kerakerala
A
14

Another way to get the number of subelements:

len(list(root))
Adelbert answered 28/8, 2017 at 5:24 Comment(1)
Note: len(element) would work as well. No need for list(), nor is this restricted to using it on the root.Hainan
S
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
Strive answered 2/3, 2017 at 10:5 Comment(0)
C
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")
Cubit answered 23/1, 2018 at 20:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.