简体   繁体   中英

Is there a way to count the number of elements of a certain name in an xml file using Python?

I'm using Python 3.4 on a Windows 64-bit machine.

I currently have a xml file which has multiple hierarchies. There are a number of elements going by the name "paragraph" in the xml tree. But they might be on different hierarchies.

Is there any way to count the number of these elements in an easy way? Traversal through the whole tree seems way too time-consuming.

If you were to use lxml.etree , then you would have a full XPath support and can use count() :

import lxml.etree as ET

tree = ET.parse(xml)
paragraphs = tree.xpath('count(//p)')
print(paragraphs)

In xml.etree.ElementTree you would have to do it in Python via findall() and len() because of the limited XPath support :

import xml.etree.ElementTree as ET

tree = ET.parse(xml)
paragraphs = tree.findall('//p')
print(len(paragraphs)) 

Read the xml file and get the content in xmlString. If all you need is the number of occurrences of the word "paragraph", you can do something like this -

xmlString.count("<paragraph>")

This makes several assumptions about how your xml file looks like and may not work in all cases.

I now have found a easy approch to do the job using xml.dom.minidom :

import xml.dom.mimidom as DM
tree = DM.parse(xml_file)
paragraphs = tree.getElementByTagName('paragraph')
print(len(paragraphs))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM