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?
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?
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(".//*"))
getchildren
was deprecated in python 2.7. –
Adelbert root.getchildren()
-> list(root)
as per the docs and you can simply do len(root)
–
Machzor 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("*"))
Another way to get the number of subelements:
len(list(root))
len(element)
would work as well. No need for list()
, nor is this restricted to using it on the root
. –
Hainan 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
# 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")
© 2022 - 2024 — McMap. All rights reserved.